UILabel 设置不同颜色,不同字体
2015-07-03 16:29
441 查看
UILabel * label =[[UILabel alloc]init];
label.numberOfLines=0;//设置无线行
label.lineBreakMode=NSLineBreakByCharWrapping;//按照字符来换行
[self.view addSubview:label];
NSString * str1 =@"请问你你米库e房内功夫呢分你哦恩二娃你看法呢空风科技防控啊发饿哦几";
NSString * str2 =@"多选项2";
int length1 =str1.length;
int length2 =str2.length;
NSLog(@"%d %d",length1,length2);
NSString * allString =[NSString stringWithFormat:@"%@ %@",str1,str2];
NSLog(@"%d",allString.length);
NSMutableAttributedString *str = [[NSMutableAttributedStringalloc] initWithString:allString];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,length1)];
[str addAttribute:NSFontAttributeName value:[UIFontsystemFontOfSize:17] range:NSMakeRange(0,
length1)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(length1+2,length2+1)];
[str addAttribute:NSFontAttributeName value:[UIFontsystemFontOfSize:15] range:NSMakeRange(length1+2,length2+1)];
label.attributedText=str;
//自适应高度
CGRect rect =[allString boundingRectWithSize:CGSizeMake(200,0) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeadingattributes:@{NSFontAttributeName :
[UIFont systemFontOfSize:17]}context:nil];
label.frame=CGRectMake(10, 100, rect.size.width,
rect.size.height);
label.numberOfLines=0;//设置无线行
label.lineBreakMode=NSLineBreakByCharWrapping;//按照字符来换行
[self.view addSubview:label];
NSString * str1 =@"请问你你米库e房内功夫呢分你哦恩二娃你看法呢空风科技防控啊发饿哦几";
NSString * str2 =@"多选项2";
int length1 =str1.length;
int length2 =str2.length;
NSLog(@"%d %d",length1,length2);
NSString * allString =[NSString stringWithFormat:@"%@ %@",str1,str2];
NSLog(@"%d",allString.length);
NSMutableAttributedString *str = [[NSMutableAttributedStringalloc] initWithString:allString];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,length1)];
[str addAttribute:NSFontAttributeName value:[UIFontsystemFontOfSize:17] range:NSMakeRange(0,
length1)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(length1+2,length2+1)];
[str addAttribute:NSFontAttributeName value:[UIFontsystemFontOfSize:15] range:NSMakeRange(length1+2,length2+1)];
label.attributedText=str;
//自适应高度
CGRect rect =[allString boundingRectWithSize:CGSizeMake(200,0) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeadingattributes:@{NSFontAttributeName :
[UIFont systemFontOfSize:17]}context:nil];
label.frame=CGRectMake(10, 100, rect.size.width,
rect.size.height);
相关文章推荐
- IOS入门学习笔记(普通UI控件--UILabel)
- JS 前端框架8 require.js使用
- Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for pr
- 【Streaming】我在拓扑中使用了Ack机制,为什么在Storm UI上有大量Failed数据?
- JS 前端框架7 一个简单的require功能
- Easy Ui 加Loading
- error C2440: ’static_cast’ : cannot convert from ‘UINT (__thiscall CStaticLink::* )(CPoint)’ to ‘LRE
- Andriod UiAutomator 使用 Jenkins 实现持续集成
- NFinal学习笔记 02—NFinalBuild
- IOS自动化测试之UIAutomation Test学习——first step:Instrument
- NSOperationQueue和GCD的区别,以及在什么场合下使用
- Android酷炫实用的开源框架(UI框架)
- 主动呼叫KVO(NSKeyValueObserverNotification)
- 关于${pageContext.request.contextPath}的理解
- FlexBuilder无法连接Flashplayer调试器进行DEBUG
- ARP协议详解之Gratuitous ARP(免费ARP)
- UICollectionView基础
- web项目编译出错时,原因之一,可能是build path 中order and Export引起
- UITableViewCell自动调整高度相关资料整理
- ARP协议详解之Gratuitous ARP(免费ARP)