您的当前位置:首页正文

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

来源:华佗小知识

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]];