300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > iOS 富文本~设置字体大小和颜色

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

时间:2020-04-10 22:46:19

相关推荐

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

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

一、富文本效果图:

图1:

二、富文本实现代码:

//1.初始化UILabel

UILabel *introLabel = [UILabel lableFrame:CGRectZero title:nil backgroundColor:[UIColor clearColor] font:kGlobalFontSize_28 textColor:[UIColor purpleColor]];

//2.初始化富文本

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

introLabel.attributedText = indroStr;

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

[self addSubview:introLabel];

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

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

//分类

#pragma mark - 创建UILabel

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

{

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

lable.text=title;

lable.font=font;

[lable setBackgroundColor:color];

lable.textColor=textColor;

return lable;

}

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

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

NSRange hightlightTextRange = [text rangeOfString:rangeText];

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

if (hightlightTextRange.length > 0) {

[attributeStr addAttribute:NSForegroundColorAttributeName

value:color

range:hightlightTextRange];

[attributeStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14.0f] range:hightlightTextRange];

return attributeStr;

}else {

return [rangeText copy];

}

}

谢谢!!!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。