1 textAlignment有三种设置方式:(以UI开头,是ios6之前的用法)
(1) NSTextAlignmentLeft 为向左对齐
(2) NSTextAlignmentCenter 为居中对齐
(3) NSTextAlignmentRight 为向右对齐
2 linBreakMode(可选值) (当文本内容很多,label无法全部显示时label会将文本内容以省略号的方式代替)
enum{
NSLineBreakByWordWrapping = 0 //保留整个单词,以空格为边界
NSLineBreakByCharWrapping //保留整个字符
NSLineBreakByClipping //以边界为止
NSLineBreakByTruncatingHead //省略开头,以省略号代替
NSLineBreakByTruncatingTail //省略结尾,以省略号代替
NSLineBreakByTruncatingMiddle //省略中间,以省略号代替
}
3 //创建label
UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake( x, y, width, height)];
//标题
1 label.text=@"123";
2 NSString *labelText = @"123"; label.text = labelText;
//label字体大小(系统默认)
label.font =[UIFont systemFontOfSize:12.0f];
//文字类型及大小
label.font = [UIFont fontWithName:@"Arial" size:30];
//字体颜色
label.textColor =[UIColor blueColor];
//设置label的旋转角度
label.transform = CGAffineTransformMakeRotation(0.1);
//设置阴影的倾斜角度即大小
label.shadowOffset = CGSizeMake(2.0f, 2.0f);
//设置圆角及其透明度
label.layer.cornerRadius=6;
//设置文本的对齐方式
label.textAlignment = NSTextAlignmentLeft;
//label的背景颜色
label.backgroundColor = [UIColor clearColor];
//保留整个文本字符
label.lineBreakMode =NSLineBreakByCharWrapping;
//指定换行模式
label.lineBreakMode = UILineBreakModeWordWrap;
// 指定label的行数
label.numberOfLines = 2;
//设置文本的阴影色彩和透明度
label.shadowColor = [UIColorcolor WithWhite:0.1 falpha:0.8f];
label.shadowColor =[UIColor grayColor];
//设置高亮
label.highLighted =YES;
//label的标记
label.tag=20;
//把label放到view上
[self.view addSubview:label];
4 设置label的边框粗细与颜色,设置前添加头文件#import<QuartzCore/QuartzCore.h>
(1) label.layer.borderColor = [UIColor lightGrayColor].CGColor; //边框颜色,要为CGColor
(2) label.layer.borderWidth = 1;//边框宽度
5 设置label背景图
(1) UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"itemkaung2.png"]];
[myLabel setBackgroundColor:color];
(2) UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 200, 400)];
UIImageView *imageView =[[UIImageView alloc] init];
imageView.frame =CGRectMake(50, 50, 200, 400);
UIImage *image=[UIImage imageNamed:@"1.jpg"];
imageView.image =image;
label.backgroundColor = [UIColor clearColor];
label.text =@"hello world";
label.font = [UIFont systemFontOfSize:30];
label.textColor = [UIColor yellowColor];
[self.view addSubview:imageView];//添加的顺序不能错,否则图片会覆盖label
[self.view addSubview:label];
(3) UIColor * color = [UIColor colorWithPatternImage:image];//image为需要添加的背景图
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 200)];
[label setBackgroundColor:color];
[self.view addSubview:label];
提示:
有缺陷,当背景图的尺寸与label大小不一致时,会出现背景图被部分截取或者平铺重复的情况,所以需要大小一致再设置 背景颜色。可以用下面的函数设置image尺寸
-(UIImage *)scaleImage:(UIImage *)img ToSize:(CGSize)itemSize
{
UIImage *i;
// 创建一个bitmap的context,并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect=CGRectMake(0, 0, itemSize.width, itemSize.height);
// 绘制改变大小的图片
[img drawInRect:imageRect];
// 从当前context中创建一个改变大小后的图片
i=UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
return i;
}
然后在主函数中调用即可
CGSize size= CGSizeMake(100, 200);
UIImage *image =[UIImage imageNamed:@"1.jpg"];
UIImage *laterImage =[self scaleImage:image ToSize:size];
UIColor * color = [UIColor colorWithPatternImage:laterImage];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 200)];
[label setBackgroundColor:color];
[self.view addSubview:label];
设置label背景图部分可以链接: http://blog.csdn.net/changesquare/article/details/11353413
原文:http://www.cnblogs.com/luoyubuku/p/3816922.html