1、 UILabel 使用 NSString 显示文字------代码创建Label
{
//定义标签的位置和尺寸,位置:x=0.0f, y=0.0f ,尺寸:x=320.0f, y=30.0f
UILabel *Label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 30.0f)];
//建立 NSString,定义其内容为“abc”
NSString *sampleText = [NSString stringWithFormat:@"abc"];
//使用UILabel Label 获取sampleText
Label.text = sampleText;
//输出结果
NSLog(@“%@\n”,sampleText);
//在 View 的 subview 中加入标签
[self.view addSubview:Label];
//使用后释放Label
[Label release];
}
//设置显示文字
scoreLabel.text = @"我是Andy--清风";
//设置字体:粗体,正常的是 SystemFontOfSize,调用系统的字体配置
scoreLabel.font = [UIFont boldSystemFontOfSize:20];
//设置文字颜色,可以有多种颜色可以选择
scoreLabel.textColor = [UIColor orangeColor];
scoreLabel.textColor = [UIColor purpleColor];
//设置文字对齐位置,居左,居中,居右
scoreLabel.textAlignment = UITextAlignmentRight;
scoreLabel.textAlignment = UITextAlignmentCenter;
//设置字体大小是否适应label宽度
scoreLabel.adjustsFontSizeToFitWidth = YES;
//设置label的行数,这个可以根据上节的UITextView自适应高度
scoreLabel.numberOfLines = 2;
//设置文本是否高亮和高亮时的颜色
scoreLabel.highlighted = YES;
scoreLabel.highlightedTextColor = [UIColor orangeColor];
//设置阴影的颜色和阴影的偏移位置
scoreLabel.shadowColor = [UIColor redColor];
scoreLabel.shadowOffset = CGSizeMake(1.0,1.0);
//设置是否能与用户进行交互
scoreLabel.userInteractionEnabled = YES;
//设置label中的文字是否可变,默认值是YES
scoreLabel.enabled = NO;
//设置文字过长时的显示格式
scoreLabel.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间
//设置背景色为透明
scoreLabel.backgroudColor=[UIColor clearColor];
2、让label自适应里面的文字,自动调整宽度和高度。
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];这个frame是初设的,没关系,后面还会重新设置其size。
[label setNumberOfLines:0];
NSString *s = @"string......";
UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];
[self.view addSubView:label];
这样就可以对s赋值让其自动调整其大小了。其实宽度可以设置为固定,让其改高度更好。