NSString*string = @"如何使得Label里的字符有不同的颜色?"; NSRange range = [string rangeOfString: @"Label"]; NSMutableAttributedString*attribute = [[NSMutableAttributedStringalloc] initWithString: string]; [attribute addAttributes: @{NSForegroundColorAttributeName: [UIColorredColor]}range: range]; [attribute addAttributes: @{NSForegroundColorAttributeName: [UIColorgreenColor]}range: NSMakeRange(0, range.location)]; [attribute addAttributes: @{NSForegroundColorAttributeName: [UIColorcyanColor]}range: NSMakeRange(range.location+ range.length, 5)]; UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0.0f, 100.0f, 320.0f, 100.0f)]; [label setText: string]; [label setAttributedText: attribute];
字符串显示不同的颜色
最新推荐文章于 2019-12-03 11:02:37 发布