- 实际上 label 就是一个可以显示文字的视图控件。
1、Label 的创建
复制
UILabel *label = [[UILabel alloc] init];
[self.view addSubview:label];
label.frame = CGRectMake(10, 40, 100, 25);
2、Label 的设置
label.text = @"LabelLabelLabelLabelLabelLabelLabelLabelLabelLabel";
label.backgroundColor = [UIColor redColor];
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentCenter;
label.numberOfLines = 0;
label.shadowColor = [UIColor greenColor];
label.shadowOffset = CGSizeMake(5, 5);
label.lineBreakMode = NSLineBreakByWordWrapping;
label.font = [UIFont systemFontOfSize:30];
label.font = [UIFont boldSystemFontOfSize:30];
label.font = [UIFont italicSystemFontOfSize:30];
label.font = [UIFont fontWithName:@"Zapfino" size:15];
NSArray *fontNameArray = [UIFont familyNames];
label.adjustsFontSizeToFitWidth = YES;
[label sizeToFit];
label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
3、可变属性 Label 的创建
NSString *str1 = @"NSMutable";
NSString *str2 = @"Attributed";
NSString *str3 = @"String";
NSRange range1 = NSMakeRange(0, str1.length);
NSRange range2 = NSMakeRange(str1.length, str2.length);
NSRange range3 = NSMakeRange(str1.length + str2.length, str3.length);
NSMutableAttributedString *str = [[NSMutableAttributedString alloc]
initWithString:[NSString stringWithFormat:@"%@%@%@", str1, str2, str3]];
[str addAttributes:@{NSForegroundColorAttributeName:[UIColor blueColor],
NSFontAttributeName:[UIFont boldSystemFontOfSize:15]}
range:range1];
[str addAttributes:@{NSForegroundColorAttributeName:[UIColor brownColor],
NSFontAttributeName:[UIFont systemFontOfSize:40]}
range:range2];
[str addAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor],
NSFontAttributeName:[UIFont italicSystemFontOfSize:25]}
range:range3];
label.attributedText = str;
[label sizeToFit];
4、常见需求
UIView *backView = [[UIView alloc] init];
[self.view addSubview:backView];
backView.frame = CGRectMake(10, 30, 300, 50);
backView.backgroundColor = [UIColor grayColor];
UILabel *label = [[UILabel alloc] init];
[backView addSubview:label];
label.frame = CGRectMake(0, 10, 120, 30);
label.backgroundColor = [UIColor yellowColor];
label.textColor = [UIColor greenColor];
label.text = @"Label Marquee";
backView.clipsToBounds = YES;
CGRect frame = label.frame;
frame.origin.x = backView.frame.size.width;
label.frame = frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationRepeatCount:CGFLOAT_MAX];
frame.origin.x = -frame.size.width;
label.frame = frame;
[UIView commitAnimations];

@interface UILabel (Extension)
@property (nonatomic) NSString *verticalText;
@end
#import "UILabel+Extension.h"
#import "objc/Runtime.h"
@implementation UILabel (Extension)
- (NSString *)verticalText {
return objc_getAssociatedObject(self, @selector(verticalText));
}
- (void)setVerticalText:(NSString *)verticalText {
objc_setAssociatedObject(self, &verticalText, verticalText, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
NSMutableString *str = [[NSMutableString alloc] initWithString:verticalText];
NSInteger count = str.length;
for (NSInteger index = 1; index < count; index ++) {
[str insertString:@"\n" atIndex:index * 2 - 1];
}
self.text = str;
self.numberOfLines = 0;
}
@end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)