美文网首页
iOS 之label简单设置

iOS 之label简单设置

作者: MonkeysAndTyper | 来源:发表于2016-05-09 20:00 被阅读0次

1 label的宽度自适应

CGFloat height = 30;
CGRect newRect = [self.labelOfMerName.text boundingRectWithSize:CGSizeMake(0, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];

if (newRect.size.width > 220) {
    newRect.size.width = 220;
}

self.labelOfMerName.frame = CGRectMake(50, 20, newRect.size.width, 30);

2 label的高度自适应

CGFloat width = 30;
CGRect newRect = [self.labelOfMerName.text boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];

3 简单富文本(高亮)

    NSRange range = [strOne rangeOfString:strTwo];
    NSString *str = [NSString stringWithFormat:@"%@", strOne];
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:str];
    [attribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:range];
    [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    [textLabel setAttributedText:attribute];

相关文章

网友评论

      本文标题:iOS 之label简单设置

      本文链接:https://www.haomeiwen.com/subject/llmerttx.html