在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的创建。
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 200, 400)];
[self.view addSubview:label];
或者用下面的代码来改变label的大小
label.frame = CGRectMake(97, 47, 223, 19);
label.tag =101;
label.text =@"abcd" 或者
NSString *labelText = @"abcd";
label.text = labelText;
label.font = [UIFont systemFontOfSize:12];//采用系统默认文字设置大小
label.font = [UIFont fontWithName:@"Arial" size:30];//设置文字类型与大小
label.textColor = [UIColor lightGrayColor];//其中textColor要用UIColor类型
label.textAlignment = NSTextAlignmentLeft;
其中textAlignment有三种设置方式:NSTextAlignmentLeft为向左对齐,NSTextAlignmentCenter为居中对齐,NSTextAlignmentRight为向右对齐
如果有一些文章介绍时用的是UITextAlignmentCenter/UITextAlignmentLeft/UITextAlignmentRight,那是iOS6以前的用法,iOS6的最新用法已改当文本内容很多,label无法全部显示时label会将文本内容以省略号的方式代替,下面说一下label文本省略方式的设置
label.lineBreakMode =NSLineBreakByCharWrapping;//其中lineBreakMode可选值为
linBreakMode enum{
NSLineBreakByWordWrapping = 0,//保留整个单词,以空格为边界
NSLineBreakByCharWrapping,//保留整个字符
NSLineBreakByClipping,//以边界为止
NSLineBreakByTruncatingHead,//省略开头,以省略号代替
NSLineBreakByTruncatingTail,//省略结尾,以省略号代替
NSLineBreakByTruncatingMiddle//省略中间,以省略号代替
}
label.numberOfLines = 1;//行数设置为1,不设置时系统会默认行数为1
[label sizeToFit];
commentTextLabel.lineBreakMode = NSLineBreakByCharWrapping;
commentTextLabel.numberOfLines = 0;
label.numberOfLines =1;
label.adjustsFontSizeToFitWidth =YES;
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(100, 180) lineBreakMode:NSLineBreakByCharWrapping];
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];
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.layer.borderColor = [UIColor lightGrayColor].CGColor;//边框颜色,要为CGColor
label.layer.borderWidth = 1;//边框宽度
label.backgroundColor =[UIColor yellowColor];
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 * color = [UIColor colorWithPatternImage:image];//image为需要添加的背景图
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 200)];
[label setBackgroundColor:color];
[self.view addSubview:label];
-(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.highLighted =YES;
label.shadowColor =[UIColor grayColor];
label.shadowOffset = CGSizeMake(2.0, 2.0);
label.layer.cornerRadius = 10;
转自:http://blog.csdn.net/changesquare/article/details/11353413