iOS 之label简单设置

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 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,229评论 4 61
  • 爱就是允许。爱一个人,不论是你的老公还是你的孩子,真正的爱他们,就是完全的允许他们做任何他们想做的事。就像你爱自然...
    吴荻之阅读 363评论 0 1
  • 在当下互联网+的潮流感染下,对传统行业互联网思维化或传统行业互联网转型的思考也是偶有想法。 比如,曾经服务过的中石...
    Tina_mama阅读 265评论 0 2
  • 第六章 一本书的分类 分析阅读的第一个规则是,你一定要知道自己在读的是哪一类的书,而且要越早知道越好,最好早在你开...
    喵皇后阅读 166评论 1 1
  • 一座城市既可以热情奔放又兼具端庄典雅,既有青春的朝气又有岁月的沉淀,那一定是巴塞罗那。香烟啤酒,栗色麦穗秀发和皮衣...
    行走的虾米阅读 569评论 0 1