UILabel使用

UILable 字体加粗
加粗;
[UILabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];

加粗并且倾斜
[UILabel setFont:[UIFont fontWithName:@"Helvetica-BoldOblique" size:20]];

UILabel自适应高度 换行
//初始化label  
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];  
//设置自动行数与字符换行  
[label setNumberOfLines:0];  
label.lineBreakMode = NSLineBreakByCharWrapping;   
// 测试字串  
NSString *s = @"这是一个测试!!!adsfsaf时发生发勿忘我勿忘我勿忘我勿忘我勿忘我阿阿阿阿阿阿阿阿阿阿阿阿阿00000000阿什顿。。。";  
UIFont *font = [UIFont fontWithName:@"Arial" size:12];  
//使用这中方法需要font相同
label.font=font;
//设置一个行高上限  
CGSize size = CGSizeMake(320,2000);  
//计算实际frame大小,并将label的frame变成实际大小  
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];  
[label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];


关键字标红

 
NSString *keyword = @"开源";
NSString *result = @"开源中国社区";

// 设置标签文字
NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc]initWithString:result];

// 获取标红的位置和长度
NSRange range = [result rangeOfString:keyword];

// 设置标签文字的属性
[attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],   NSFontAttributeName : [UIFont systemFontOfSize:17]} range:range];

// 显示在Label上
label.attributedText = attrituteString;

//设置标签文本(此属性是iOS6.0之后才出现,如若不是必要,不建议使用此属性)
NSString *text = @"其实没什么";
    
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];
    
[attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],	NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(2, 1)];
    
myLabel.attributedText = attributeString;




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值