//聯(lián)系人:石虎QQ:1224614774昵稱:嗡嘛呢叭咪哄
一威鹿、富文本效果圖:
圖1:
二、富文本實(shí)現(xiàn)代碼:
//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];
//注意:效果是@“張瓊在新神力酒店上班”可以設(shè)置一個顏色,這字符串中的@“張瓊”可以設(shè)置其它的顏色和字體大小.
三析砸、富文本實(shí)現(xiàn)的分類方法
//分類
#pragma mark - 創(chuàng)建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 -富文本設(shè)置部分字體顏色
+ (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];
}}