1 textAlignment有三種設置方式:(以UI開頭,是ios6以前的用法)ios
(1) NSTextAlignmentLeft 爲向左對齊app
(2) NSTextAlignmentCenter 爲居中對齊函數
(3) NSTextAlignmentRight 爲向右對齊字體
2 linBreakMode(可選值) (當文本內容不少,label沒法所有顯示時label會將文本內容以省略號的方式代替)spa
enum{ .net
NSLineBreakByWordWrapping = 0 //保留整個單詞,以空格爲邊界 orm
NSLineBreakByCharWrapping //保留整個字符 blog
NSLineBreakByClipping //以邊界爲止 圖片
NSLineBreakByTruncatingHead //省略開頭,以省略號代替 ip
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