UILabel控件
- (void)viewDidLoad {
[super viewDidLoad];
//初始化label
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(50, 50, 150, 100);
label.text = @"what the fack";
//设置阴影
// label.shadowColor = [UIColor redColor];
// label.shadowOffset = CGSizeMake(1.0,1.0);
// 设置高亮
// label.highlighted = YES;
// label.highlightedTextColor = [UIColor orangeColor];
// 设置文本字体和大小
label.font = [UIFont fontWithName:@"Arial" size:15];
label.font = [UIFont systemFontOfSize:15];
label.font = [UIFont boldSystemFontOfSize:15]; // 粗体
// 设置字体颜色
label.textColor = [UIColor orangeColor];
//设置背景颜色
label.backgroundColor = [UIColor blueColor];
//设置字体居中
/**
NSTextAlignmentLeft = 0, // Visually left aligned
NSTextAlignmentCenter = 1, // Visually centered
NSTextAlignmentRight = 2, // Visually right aligned
*/
label.textAlignment = NSTextAlignmentCenter;
//设置单词折行方式
/**
NSLineBreakByWordWrapping = 0,default 以单词为单位换行,以单位为单位截断。
NSLineBreakByCharWrapping,//以字符为单位换行,以字符为单位截断。
NSLineBreakByClipping, //以单词为单位换行。以字符为单位截断。
NSLineBreakByTruncatingHead,//Truncate at head of line: "...wxyz"
NSLineBreakByTruncatingTail,//Truncate at tail of line: "abcd..."
NSLineBreakByTruncatingMiddle//Truncate middle of line: "ab...yz"
*/
// label.lineBreakMode = NSLineBreakByWordWrapping;
//设置label是否可以显示多行,0则显示多行
label.numberOfLines = 0;
//设置字体大小适宜label宽度
label.adjustsFontSizeToFitWidth = YES;
//设置是否能与用户交互
label.userInteractionEnabled = YES;
[self.view addSubview:label];
/**
uilabel的Attributed用法
*/
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 300, 300)];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"This is my test code to test this label style is working or not on the text to show other user"];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,31)];
[str addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(61,10)];
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica-Bold" size:13.0] range:NSMakeRange(32, 28)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:13.0] range:NSMakeRange(65, 20)];
label1.attributedText = str;
//设置label是否可以显示多行,0则显示多行
label1.numberOfLines = 0;
[self.view addSubview:label1];
}