美文网首页iOS开发
IOS Nsstring 同一行不同的颜色和大小

IOS Nsstring 同一行不同的颜色和大小

作者: JakieZhang | 来源:发表于2016-12-05 15:15 被阅读105次

1. 不同大小

NSString*needText =@"你好啊啊(11)";

[self.mylabel  setAttributedText:[selfchangeLabelWithText:needText]];

-(NSMutableAttributedString*) changeLabelWithText:(NSString*)needText

{

NSMutableAttributedString*attrString = [[NSMutableAttributedStringalloc]initWithString:needText];

UIFont*font = [UIFontsystemFontOfSize:20];

[attrStringaddAttribute:NSFontAttributeNamevalue:fontrange:NSMakeRange(0,4)];

[attrStringaddAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:12]range:NSMakeRange(4,needText.length-4)];

returnattrString;

}

2. 不同颜色

NSMutableAttributedString* attr = [[NSMutableAttributedStringalloc]initWithString:scoreStr];

[attraddAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:15],NSForegroundColorAttributeName:HEXCOLOR(0x4b5056)}range:NSMakeRange(0, scoreStr.length)];

[attraddAttribute:NSForegroundColorAttributeNamevalue:HEXCOLOR(0xdd5365)range:[scoreStrrangeOfString:score]];

[attraddAttribute:NSForegroundColorAttributeNamevalue:HEXCOLOR(0x7e848a)range:[scoreStrrangeOfString:ahead]];

相关文章

网友评论

    本文标题:IOS Nsstring 同一行不同的颜色和大小

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