iOS:UILabel设置不同字体颜色

来源:互联网 发布:网站源码音乐连接 编辑:程序博客网 时间:2024/06/11 15:32
NSString *str = @"0123456789”;//label内容NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:str];//根据下标索引设置字体颜色[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];//根据下标索引设置字体样式大小(式样只对字母有效)[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];_myLabel.attributedText = str1;//以上方法为iOS6之后,iOS6之前需要使用到一个第三方库TTTAttributedLabel,同时还有导入CoreText.frame框架

0 0
原创粉丝点击