// 设置标签显示文本
@property(nullable, nonatomic, copy) NSString *text;
// 设置文本字体和字体大小
@property(null_resetable, nonatomic, strong) UIFont *font;
// 设置文本颜色
@property(nullable, nonatomic, copy) UIColor *backgroundColor;
// 设置文本对齐方式,默认居左(NSTextAlignmentLeft)
@property(nonatomic) NSTextAlignment textAlignment;
// 设置超出label边界文字的截取方式(默认省略结尾)。
@property(nonatomic) NSLineBreakMode lineBreakMode;
// 设置文本是否可高亮
@property(nonatomic, getter=isHighlighted) BOOL highlighted;
// 设置用于渲染文本的最大行数。
@property(nonatomic) NSInteger numberOfLines;
UILable标签的创建
-(void)viewDidLoad {
[super viewDidLoad];
//
UILabel *label = [[UIabel alloc] initWithFrame:CGRectMake(50, 100, 250, 50)];
label.backgroundColor = [UIColor redColor];
label.textColor = [UIColor whiteColor];
label.text = @"www.csdn.com";
label.font = [UIFont systemFontOfSize:20];
label.textAlignment = NSTextAligenmentCenter;
[self.view addSubview:label];
}
// 一个UILabel中显示不同的颜色或不同的字体文字。可以使用NSAttributedText和NSMutableAttributedText类,并设置UILabel的attributedText来实现上述效果.
-(void)viewDidLoad {
[super viewDidLoad];
//
UILabel *label = [[UIabel alloc] initWithFrame:CGRectMake(20, 100, [UIScreen mainScreen].bounds.size.width-40, 30)];
label.backgroundColor = [UIColor redColor];
label.textColor = [UIColor whiteColor];
label.text = @"www.csdn.com";
label.font = [UIFont systemFontOfSize:20];
label.textAlignment = NSTextAligenmentCenter;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 4)];
[attributedString addAttribute:NSFontgroundColorAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30] range:NSMakeRange(0, 4)];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(12, 5)];
[attributedString addAttribute:NSFontgroundColorAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:25] range:NSMakeRange(12, 5)];
label.attributedText = attributedString;
[self.view addSubview:label];
}