美文网首页
IOS开发-UILabel自定义样式--同一个Label中不同颜

IOS开发-UILabel自定义样式--同一个Label中不同颜

作者: swluan | 来源:发表于2016-12-29 14:52 被阅读0次

有时会遇到同一个Label需要包含不同颜色、字体的内容的需求,方法如下

NSMutableAttributedString*sizeStr = [[NSMutableAttributedStringalloc]initWithString:[NSStringstringWithFormat:@"FRAMED SIZE:%@",@"40\"x30\""]];

//需要改变的第一个文字的位置

NSUIntegerfirstLoc = [[sizeStrstring]rangeOfString:@":"].location+1;

//需要改变的最后一个文字的位置

NSUIntegerlengthLoc = [sizeStrstring].length;

//需要改变的区间

NSRangerange =NSMakeRange(firstLoc, lengthLoc - firstLoc);

//改变颜色

[sizeStraddAttribute:NSForegroundColorAttributeNamevalue:[UIColorcolorWithHexString:@"#000000"]range:range];

//改变字体大小及类型

[sizeStraddAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"Helvetica-BoldOblique"size:15]range:range];

//为label添加Attributed

[frameSizelabelsetAttributedText:sizeStr];

相关文章

网友评论

      本文标题:IOS开发-UILabel自定义样式--同一个Label中不同颜

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