iOS 富文本~设置字体大小和颜色

//联系人:石虎QQ:1224614774昵称:嗡嘛呢叭咪哄

一、富文本效果图:

图1:

二、富文本实现代码:

//1.初始化UILabel

UILabel*introLabel = [UILabellableFrame:CGRectZerotitle:nilbackgroundColor:[UIColorclearColor]font:kGlobalFontSize_28textColor:[UIColorpurpleColor]];

//2.初始化富文本

NSMutableAttributedString*indroStr = [NSMutableAttributedStringsetupAttributeString:@"张琼在新神力酒店工作"rangeText:@"张琼"textColor:OKColorRGB(236,198,128)];

introLabel.attributedText = indroStr;

introLabel.frame = CGRectMake(10,100,500,40);

[selfaddSubview:introLabel];

//注意:效果是@“张琼在新神力酒店上班”可以设置一个颜色,这字符串中的@“张琼”可以设置其它的颜色和字体大小.

三、富文本实现的分类方法

//分类

#pragma mark - 创建UILabel

+ (UILabel*)lableFrame:(CGRect)frame title:(NSString*)title backgroundColor:(UIColor*)color font:(UIFont*)font textColor:(UIColor*)textColor

{

UILabel*lable=[[UILabelalloc]initWithFrame:frame];

lable.text=title;

lable.font=font;

[lablesetBackgroundColor:color];

lable.textColor=textColor;

returnlable;

}

#pragma mark -富文本设置部分字体颜色

+ (NSMutableAttributedString*)setupAttributeString:(NSString*)text rangeText:(NSString*)rangeText textColor:(UIColor*)color  {

NSRangehightlightTextRange = [textrangeOfString:rangeText];

NSMutableAttributedString*attributeStr = [[NSMutableAttributedStringalloc]initWithString:text];

if(hightlightTextRange.length>0) {

[attributeStraddAttribute:NSForegroundColorAttributeName

value:color

range:hightlightTextRange];

[attributeStraddAttribute:NSFontAttributeNamevalue:[UIFontboldSystemFontOfSize:14.0f]range:hightlightTextRange];

returnattributeStr;

}else{

return[rangeTextcopy];

}}

谢谢!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容