UILabel - 方法总结

本文详述了UILabel的初始化、文字设置、匹配文字、布局调整等方面,包括设置默认文本、字体、颜色、对齐方式,以及如何适应字体大小、行数、高亮和阴影效果。还介绍了如何处理自定义字体的加载和使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、初始化

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 44)];
     
[self.view addSubview:myLabel];

二、设置文字

1、设置默认文本

NSString *text = @"标签文本";
myLabel.text = text;

效果:

20161892441299.png (87×39)


2、设置标签文本

(此属性是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;

效果:

20161892517640.png (98×39)


关键字标红的效果

NSString *keyword = @"你好";
NSString *result = @"你好世界";
 
// 设置标签文字
NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] init
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值