UILabel也是相当常用的控件,主要是用来显示一段文字的,因此常用方法也是对控件内文字的一些设置。
1 //文本标签 2 UILabel *label = [[UILabel alloc]init]; 3 label.frame = CGRectMake(10, 100, 355, 300); 4 label.backgroundColor = [UIColor yellowColor]; 5 //文本 6 label.text = @"Hello.iOSghuirhgoireghoieughsiuhgfsuidhgiusfdghfidsughfdiushvdfisubvusfhjifohweuhfueoighreugh"; 7 //文字布局模式 8 label.textAlignment = NSTextAlignmentCenter; 9 //文字颜色 10 /* 11 clearColor - 透明色 12 */ 13 label.textColor = [UIColor colorWithRed:0.24 green:0.12 blue:0.51 alpha:1]; 14 //label.alpha = 0.5; 15 //字体的设置 16 label.font = [UIFont systemFontOfSize:25]; 17 //字体的加粗或者倾斜 18 label.font = [UIFont boldSystemFontOfSize:25]; 19 label.font = [UIFont italicSystemFontOfSize:25]; 20 for (NSString *name in [UIFont familyNames]) { 21 NSLog(@"%@",name); 22 } 23 label.font = [UIFont fontWithName:@"Bodoni 72 Oldstyle" size:25]; 24 //设置阴影 25 label.shadowColor = [UIColor redColor]; 26 label.shadowOffset = CGSizeMake(-1,-1); 27 //1.label要有足够的大小 28 //2.设置换行模式 29 label.lineBreakMode = NSLineBreakByWordWrapping; 30 //3.设置显示的行数(0可以不限制行数) 31 label.numberOfLines = 0; 32 //**根据字符串大小计算label的大小 33 CGSize size = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(355, 10000) lineBreakMode:NSLineBreakByCharWrapping]; 34 label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, size.height); 35 [self.view addSubview:label];
时间: 2024-10-10 20:46:22