ios,IOS 评论label Label 不敢说万能,但是绝

2017-08-31  本文已影响57人  山野不村夫

label,label在开发中有时候能要了老命,一怒之下就写了这个Label,Label ,Label  不废话,直接上图看效果,

下面直接上demo

NSString* str1 = @"隔壁老王";

NSString* str2 = @"回复";

NSString* str3 = @"隔壁老李";

NSString* str4 = @"该好好练练腰了,腰好才是真的好,腰好才是真的好,腰好才是真的好";

NSMutableAttributedString* attString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@%@%@",str1,str2,str3,str4]];

[attString addAttribute:NSForegroundColorAttributeName value:[UIColor purpleColor] range:NSMakeRange(0,str1.length)];

[attString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(str1.length,str2.length)];

[attString addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(str1.length+str2.length,str3.length)];

[attString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(str1.length+str2.length+str3.length,str4.length)];

[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:10.0] range:NSMakeRange(0, str1.length)];

[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:12.0] range:NSMakeRange(str1.length,str2.length)];

[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:10.0] range:NSMakeRange(str1.length+str2.length,str3.length)];

[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:12.0] range:NSMakeRange(str1.length+str2.length+str3.length,str4.length)];

UILabel* labelAttr = [[UILabel alloc]init];

labelAttr.backgroundColor = [UIColor lightGrayColor];

labelAttr.attributedText = attString;

labelAttr.numberOfLines = 0;

labelAttr.lineBreakMode = NSLineBreakByTruncatingTail;

CGSize max = CGSizeMake(300, 99999);

CGSize ex = [labelAttr sizeThatFits:max];

labelAttr.frame = CGRectMake(50, 100, ex.width, ex.height);

[self.view addSubview:labelAttr];

你随便想怎么改就怎么改,就这么简单,粗暴

上一篇下一篇

猜你喜欢

热点阅读