您的位置:首页 > 产品设计 > UI/UE

iOS开发笔记--UILabel的相关属性设置

2015-01-15 15:43 477 查看
在iOS编程中UILabel是一个常用的控件,下面分享一下UILabel的相关属性设置的方法。

很多学习iOS6编程都是从storyboard开始,用到UILabel时是将控件拖到storyboard中生成实现,如果想要在-(void)viewDidLoad中用代码如[_label
initWithFrame:CGRectMake(X,Y,WIDTH,HEIGHT)]方法改变拖拽到storyboard的label的大小是行不通的,因为程序加载时先执行了-(void)viewDidLoad的代码,然后再加载storyboard,而storyboard会按照拖拽控件时设置的大小来生成label,即覆盖了在-(void)viewDidLoad中设置的大小,所以要动态设置label的大小应该用代码实现UILabel的创建

[objc] view
plaincopy





UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 200, 400)];

[self.view addSubview:label];

这样就用代码实现了label的创建,其中initWithFrame设置了label的位置还有大小,其中CGRectMake可以通过另外声明CGRect rect =CGRectMake(50,50,200,400)再将变量rect放在方法initWithFrame后实现CGRectMake的四个数值分别代表rect的位置坐标x值,坐标y值,宽度width,高度height。第二句[self.view
addSubview:label]就是在当前的视图self.view中通过调用addSubview方法加入子视图,就像贴纸一样贴上去,会因为添加的顺序不同而发生后一个添加的子视图遮盖前一个子视图的现象,这个在后面讲为label添加背景图时还会提到。

或者用下面的代码来改变label的大小

[objc] view
plaincopy





label.frame = CGRectMake(97, 47, 223, 19);

设置label的标记(tag)

[objc] view
plaincopy





label.tag =101;

设置label的文本内容

[objc] view
plaincopy





label.text =@"abcd" 或者

NSString *labelText = @"abcd";

label.text = labelText;

把字符串的值赋给label

设置label的文字类型与大小

[objc] view
plaincopy





label.font = [UIFont systemFontOfSize:12];//采用系统默认文字设置大小

label.font = [UIFont fontWithName:@"Arial" size:30];//设置文字类型与大小

设置label的文字颜色

[objc] view
plaincopy





label.textColor = [UIColor lightGrayColor];//其中textColor要用UIColor类型

设置文本的对齐方式

[objc] view
plaincopy





label.textAlignment = NSTextAlignmentLeft;

其中textAlignment有三种设置方式:NSTextAlignmentLeft为向左对齐,NSTextAlignmentCenter为居中对齐,NSTextAlignmentRight为向右对齐
如果有一些文章介绍时用的是UITextAlignmentCenter/UITextAlignmentLeft/UITextAlignmentRight,那是iOS6以前的用法,iOS6的最新用法已改

当文本内容很多,label无法全部显示时label会将文本内容以省略号的方式代替,下面说一下label文本省略方式的设置

[objc] view
plaincopy





label.lineBreakMode =NSLineBreakByCharWrapping;//其中lineBreakMode可选值为

linBreakMode enum{

NSLineBreakByWordWrapping = 0,//保留整个单词,以空格为边界

NSLineBreakByCharWrapping,//保留整个字符

NSLineBreakByClipping,//以边界为止

NSLineBreakByTruncatingHead,//省略开头,以省略号代替

NSLineBreakByTruncatingTail,//省略结尾,以省略号代替

NSLineBreakByTruncatingMiddle//省略中间,以省略号代替

}

设置文本的行数

[objc] view
plaincopy





label.numberOfLines = 1;//行数设置为1,不设置时系统会默认行数为1

当需要设置的行数为不限数量的时候可以用numberOfLines=0实现

当label大小使用sizeToFit方法,调整大小时会考虑到该属性中存储的值。例如,如果此属性设置为3,sizeToFit方法会调整label使它大到足以显示三行文本。

[objc] view
plaincopy





[label sizeToFit];

实现文本多行显示

[objc] view
plaincopy





commentTextLabel.lineBreakMode = NSLineBreakByCharWrapping;

commentTextLabel.numberOfLines = 0;

文本自动根据label大小自动调整字体尺寸

[objc] view
plaincopy





label.numberOfLines =1;

label.adjustsFontSizeToFitWidth =YES;

adjustFontSizeToFitWidth方法可实现文本自动根据label大小自动调整字体尺寸,直到文本的大小达到了自己设置的label文本尺寸最大、最小值与字符串的最大最小值,要是用这个方法还有一个很大的限制就是只有在numberOfLines设置为1时才能用

如果行数是超过了1行,要实现自动调整字体大小功能,就没有可以自适应的系统方法可以使用,只有自己用代码实现,在设计时因为要考虑到手机屏幕的实际大小有限,如果字体太小会影响用户体验,所以要设置一个最小字号的判断,小于最小字号就要用到缩略显示,下面的代码中主要是用到

[objc] view
plaincopy





CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(100, 180) lineBreakMode:NSLineBreakByCharWrapping];

来得到字体在某一字号下的高度,判断与label高度是否一致,其中text是输入label的文本内容,sizWithFont设置字体,constrainedToSize设置约束文本的矩形大小参数,其中宽度要和label一致,高度设置要足够高,要比label高很多,否则会出现文本显示不全的问题,lineBreakMode的作用上文有讲过。如果算出的高度超出了label高度,就把字号以循环的方式减小直到高度符合就跳出循环。

[objc] view
plaincopy





float maxHeight =50;//设置最大高度

float minFontSize =9;

float height;

int fontSize = 31;//设置最大字号

NSString *text = @"输入文本内容";

do {

fontSize = fontSize - 1;

UIFont *font =[UIFont fontWithName:@"Arial" size:fontSize];

CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(100, 180)/*宽度与label的宽度一样,高度应高于label高度*/ lineBreakMode:NSLineBreakByCharWrapping];

height = size.height;

NSLog(@"height=%f,fontSize=%d,text=%@",height,fontSize,text);

} while (height > maxHeight&&fontSize>minFontSize);



UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];

label.text =text;

if (fontSize ==9) {//判断字体是否小于最小字号,小于最小字号时就使用系统默认的缩略显示

label.font = [UIFont fontWithName:@"Arial" size:15];

}

else{

label.font = [UIFont fontWithName:@"Arial" size:fontSize];

label.lineBreakMode = NSLineBreakByCharWrapping;//实现文字多行显示

label.numberOfLines = 0;

}

[self.view addSubview:label];

根据文本数量自动调整label高度

其实就是用上面的方法得到高度再生成label

[objc] view
plaincopy





NSString *text =[[NSString alloc]init];

text = @"输入文本内容";

CGSize size = CGSizeMake(280, 180);

UIFont *fonts = [UIFont systemFontOfSize:14.0];

CGSize msgSie = [text sizeWithFont:fonts constrainedToSize:size lineBreakMode: NSLineBreakByCharWrapping];

UILabel *textLabel = [[UILabel alloc] init];

[textLabel setFont:[UIFont boldSystemFontOfSize:14]];

textLabel.frame = CGRectMake(20,70, 280,msgSie.height);

textLabel.text = text;

textLabel.lineBreakMode = NSLineBreakByCharWrapping;//实现文字多行显示

textLabel.numberOfLines = 0;

[self.view addSubview:textLabel];

设置label的边框粗细与颜色,设置前要在相应文件中加入#import<QuartzCore/QuartzCore.h>

[objc] view
plaincopy





label.layer.borderColor = [UIColor lightGrayColor].CGColor;//边框颜色,要为CGColor

label.layer.borderWidth = 1;//边框宽度

设置label的背景颜色

[objc] view
plaincopy





label.backgroundColor =[UIColor yellowColor];

设置label背景图

设置背景图有两种方法,下面先介绍第一种方法:

设置背景图可以把一张大小与label一样的图放在label的后面一层,然后把label的背景设置为透明,这样实现label有背景

[objc] view
plaincopy





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;//imageView会根据自身大小改变添加的图片的大小所以不需要额外设置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];

这个是一个有点不正统的方法,下面要介绍更加规范的第二种方法:用UIColor设置图片,然后把UIColor作为背景颜色,就可以实现label设置背景图

[objc] view
plaincopy





UIColor * color = [UIColor colorWithPatternImage:image];//image为需要添加的背景图

UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 200)];

[label setBackgroundColor:color];

[self.view addSubview:label];

但这个方法有一个严重的缺陷,就是当背景图的尺寸与label大小不一致时,会出现背景图被部分截取或者平铺重复的情况,所以更完善的方法是要先修改好背景图的大小与label大小一致再设置背景颜色。可以用下面的函数设置image尺寸

[objc] view
plaincopy





-(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;

}

然后在主函数中调用即可

[objc] view
plaincopy





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];

设置高亮

[objc] view
plaincopy





label.highLighted =YES;

设置文本阴影

[objc] view
plaincopy





label.shadowColor =[UIColor grayColor];

设置阴影大小

[objc] view
plaincopy





label.shadowOffset = CGSizeMake(2.0, 2.0);

设置label圆角

[objc] view
plaincopy





label.layer.cornerRadius = 10;

要是用这样的设置要先在头文件中加上#import<QuartzCore/QuartzCore.h>

转自:http://blog.csdn.net/changesquare/article/details/11353413
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: