一、初始化
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 44)];
[self.view addSubview:myLabel];
二、设置文字
1、设置默认文本
NSString *text = @"标签文本";
myLabel.text = text;
效果:
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;
效果:
关键字标红的效果
NSString *keyword = @"你好";
NSString *result = @"你好世界";
// 设置标签文字
NSMutableAttributedString *attrituteString = [[NSMutableAttri

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


最低0.47元/天 解锁文章
540

被折叠的 条评论
为什么被折叠?



