UILabel 详解

一、初始化
?
1
2
3
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 44)];
     
[self.view addSubview:myLabel];
二、设置文字 

 ①、设置默认文本 

?
1
2
NSString *text = @ "标签文本" ;
myLabel.text = text;

效果:

②、设置标签文本(此属性是iOS6.0之后才出现,如若不是必要,不建议使用此属性) 
?
1
2
3
4
5
6
7
NSString *text = @ "其实没什么" ;
     
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];
     
[attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],   NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(2, 1)];
     
myLabel.attributedText = attributeString;

效果:

关键字标红的效果 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
NSString *keyword = @ "开源" ;
NSString *result = @ "开源中国社区" ;
 
// 设置标签文字
NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:result];
 
// 获取标红的位置和长度
NSRange range = [result rangeOfString:keyword];
 
// 设置标签文字的属性
[attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],   NSFontAttributeName : [UIFont systemFontOfSize:17]} range:range];
 
// 显示在Label上
label.attributedText = attrituteString;

③、设置字体,如果是使用②中的文本,那在设置AttributeString的属性时已经设置过Font了和textColor了,直接使用①设置文本时设置文本时,设置字体方法 

?
1
myLabel.font = [UIFont systemFontOfSize:13];
④、设置颜色 
?
1
myLabel.textColor = [UIColor blueColor];
⑤、设置对齐方式 
?
1
myLabel.textAlignment = NSTextAlignmentCenter; //居中
?
1
2
3
4
5
NSTextAlignmentLeft //左对齐
NSTextAlignmentCenter //居中
NSTextAlignmentRight  //右对齐
NSTextAlignmentJustified //最后一行自然对齐
NSTextAlignmentNatural //默认对齐脚本

NSTextAlignmentJustified和 NSTextAlignmentNatural用的时候会报错,程序崩溃,暂时不知道什么时候可以使用,希望知道的指教一下,感激不尽。

⑥、文字剪裁方式

?
1
2
3
4
5
6
NSLineBreakByWordWrapping = 0, //以空格为边界,保留单词
NSLineBreakByCharWrapping,    //保留整个字符
NSLineBreakByClipping,        //简单剪裁,到边界为止
NSLineBreakByTruncatingHead,  //按照"……文字"显示
NSLineBreakByTruncatingTail,  //按照"文字……文字"显示
NSLineBreakByTruncatingMiddle //按照"文字……"显示
?
1
myLabel.lineBreakMode = NSLineBreakByTruncatingHead;
⑦、设置Label enabled属性

如果设置为No,则文字颜色会变暗,表明其是不可用的,默认值为YES。

?
1
myLabel.enabled = NO;
二、匹配Label上的文字

①、是否根据文本宽度改变字体大小

?
1
2
myLabel.adjustsFontSizeToFitWidth = YES;
//假设文字内容为@"曾在月光之下望烟花,曾共看夕阳渐降下",Label长度为200,则一行显示不下,若设置此属性为YES,则会降低字体大小,以显示全部内容。
前后对比: 

       


②、改变字母之间的间距来适应label大小

?
1
2
3
4
//当这个属性是YES,标签可能改变标签文本的字母间距,以使该文本更适合标签的边界内。此属性的字符串,而不管当前行的行的裁剪模式。该属性的默认值是NO。
myLabel.adjustsLetterSpacingToFitWidth = NO;
 
//个人使用了一下,没发现有什么区别,不知道具体是什么时候发挥作用。

③、设置对齐基线

?
1
myLabel.adjustsFontSizeToFitWidth = YES; //调整基线位置需将此属性设置为YES

?
1
myLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
此属性有三个值可选 

?
1
2
3
UIBaselineAdjustmentAlignBaselines //文本最上端与Label中线对齐,默认值
UIBaselineAdjustmentAlignCenters   //文本中线与Label中线对齐
UIBaselineAdjustmentNone           //文本最下端与Label中线对齐
④、最小字体大小,当字体小于这个最小值时无效,显示此属性值 

iOS6.0之前:minimumFontSize

iOS6.0之后:minimumScaleFactor

?
1
myLabel.minimumScaleFactor = 10.0; //默认值为0,为当前字体大小

⑤、行数 

?
1
myLabel.numberOfLines = 2; //Label行数
⑥、高亮 

?
1
2
myLabel.highlighted = YES; //是否高亮
myLabel.highlightedTextColor = [UIColor redColor]; //高亮颜色;此属性在设置按钮的titleLabel时,无论highlighted是YES还是NO,在按钮按下时标题都显示此高亮颜色
⑦、阴影 

?
1
2
myLabel.shadowColor = [UIColor grayColor]; //阴影颜色,默认为nil
myLabel.shadowOffset = CGSizeMake(1, 1); //阴影的偏移点
三、Label位置 

①、计算UIlabel 随字体多行后的高度

?
1
2
3
4
CGRect result,bounds;
bounds = CGRectMake(0, 0,200, 300);
heightLabel = [myLabel textRectForBounds:bounds limitedToNumberOfLines:20]; //计算20行后的Label的Frame
NSLog(@ "%f" ,heightLabel.size.height);
②、绘制text到指定区域 

?
1
2
- ( void )drawTextInRect:(CGRect)rect
//需要重载此方法,然后由子类调用,重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了

复制代码
UILabel 多行文字自动换行 (自动折行)

1
.UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 300, 180)]; 2. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 150)]; 3. label.text = @"where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you?"; 4. //清空背景颜色 5. label.backgroundColor = [UIColor clearColor]; 6. //设置字体颜色为白色 7. label.textColor = [UIColor whiteColor]; 8. //文字居中显示 9. label.textAlignment = UITextAlignmentCenter; 10. //自动折行设置 11. label.lineBreakMode = UILineBreakModeWordWrap; 12. label.numberOfLines = 0;
复制代码

 

iOS的UILabel设置居上对齐,居中对齐,居下对齐

在iOS中默认的UILabel中的文字在竖直方向上只能居中对齐,博主参考国外网站,从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐。具体如下:

  1. //  
  2. //  myUILabel.h  
  3. //    
  4. //  
  5. //  Created by yexiaozi_007 on 3/4/13.  
  6. //  Copyright (c) 2013 yexiaozi_007. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10. typedef enum  
  11. {  
  12.     VerticalAlignmentTop = 0, // default  
  13.     VerticalAlignmentMiddle,  
  14.     VerticalAlignmentBottom,  
  15. } VerticalAlignment;  
  16. @interface myUILabel : UILabel  
  17. {  
  18. @private  
  19. VerticalAlignment _verticalAlignment;  
  20. }  
  21.   
  22. @property (nonatomic) VerticalAlignment verticalAlignment;  
  23.   
  24. @end  

 

  1. //  
  2. //  myUILabel.m  
  3. //    
  4. //  
  5. //  Created by yexiaozi_007 on 3/4/13.  
  6. //  Copyright (c) 2013 yexiaozi_007. All rights reserved.  
  7. //  
  8.   
  9. #import "myUILabel.h"  
  10.   
  11. @implementation myUILabel  
  12. @synthesize verticalAlignment = verticalAlignment_;  
  13.   
  14. - (id)initWithFrame:(CGRect)frame {  
  15.     if (self = [super initWithFrame:frame]) {  
  16.         self.verticalAlignment = VerticalAlignmentMiddle;  
  17.     }  
  18.     return self;  
  19. }  
  20.   
  21. - (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {  
  22.     verticalAlignment_ = verticalAlignment;  
  23.     [self setNeedsDisplay];  
  24. }  
  25.   
  26. - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {  
  27.     CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];  
  28.     switch (self.verticalAlignment) {  
  29.         case VerticalAlignmentTop:  
  30.             textRect.origin.y = bounds.origin.y;  
  31.             break;  
  32.         case VerticalAlignmentBottom:  
  33.             textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;  
  34.             break;  
  35.         case VerticalAlignmentMiddle:  
  36.             // Fall through.  
  37.         default:  
  38.             textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;  
  39.     }  
  40.     return textRect;  
  41. }  
  42.   
  43. -(void)drawTextInRect:(CGRect)requestedRect {  
  44.     CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];  
  45.     [super drawTextInRect:actualRect];  
  46. }  
  47.   
  48.   
  49. @end  

 

在使用时:

 

    1. lbl_mylabel = [[myUILabel alloc] initWithFrame:CGRectMake(20, 50, 150, 600)];  
    2. UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"halfTransparent.png"]];//使用半透明图片作为label的背景色  
    3. lbl_mylabel.backgroundColor = color;  
    4. lbl_mylabel.textAlignment = UITextAlignmentLeft;  
    5. lbl_mylabel.textColor = UIColor.whiteColor;  
    6. lbl_mylabel.lineBreakMode = UILineBreakModeWordWrap;  
    7. lbl_mylabel.numberOfLines = 0;  
    8. [lbl_mylabel setVerticalAlignment:VerticalAlignmentTop];  
    9. [self addSubview:lbl_mylabel]; 

 

 

ios UILabel 变量名不能为title

-[UILabel copyWithZone:]: unrecognized selector sent to instance

 
遇到了这样一个错误,找了半天没找到是什么错误,于是,Google搜索,打开第一个链接http://stackoverflow.com/questions/10784207/uilabel-copywithzone-unrecognized-selector-sent-to-instance
 
UILabel 设置过长文本中间为省略号

 label.lineBreakMode = NSLineBreakByTruncatingMiddle;

 
http://blog.csdn.net/zhaopenghhhhhh/article/details/16331041
复制代码
·UILable是iPhone界面最基本的控件,主要用来显示文本信息。
·常用属性和方法有:
1、创建
CGRect rect = CGRectMake(100, 200, 50, 50);
UILabel *label = [[UILabel alloc] initWithFrame:rect];
2、text //设置和读取文本内容,默认为nil
label.text = @”文本信息”; //设置内容
NSLog(@”%@”, label.text); //读取内容
3、textColor //设置文字颜色,默认为黑色
lable.textColor = [UIColor redColor];
4、font //设置字体大小,默认17
label.font = [UIFont systemFontOfSize:20]; //⼀一般方法
label.font = [UIFont boldSystemFontOfSize:20]; //加粗方法
label.font = [UIFont fontWithName:@"Arial" size:16]; //指定
字体的方法
//还有⼀一种从外部导入字体的方法。
5、textAlignment //设置标签文本对齐方式。
label.textAlignment = NSTextAlignmentCenter; //还有
NSTextAlignmentLeft、 NSTextAlignmentRight.
6、numberOfLines //标签最多显示行数,如果为0则表示多行。
label.numberOfLines = 2;
7、enabled //只是决定了Label的绘制方式,将它设置
为NO将会使文本变暗,表示它没有激活,这时向它设置颜色值是无效的。
label.enable = NO;
8、highlighted //是否高亮显示
label.highlighted = YES;
label.highlightedTextColor = [UIColor orangeColor]; //高亮
显示时的文本颜色
9、ShadowColor //设置阴影颜色 
[label setShadowColor:[UIColor blackColor]];
10、ShadowOffset //设置阴影偏移量
[label setShadowOffset:CGSizeMake(-1, -1)];
11、baselineAdjustment //如果adjustsFontSizeToFitWidth属性设
置为YES,这个属性就来控制文本基线的行为。
label.baselineAdjustment = UIBaselineAdjustmentNone;
UIBaselineAdjustmentAlignBaselines = 0,默认,文本最上端与中线对齐。
UIBaselineAdjustmentAlignCenters,  文本中线与label中线对齐。
UIBaselineAdjustmentNone, 文本最低端与label中线对齐。
12、Autoshrink //是否自动收缩
Fixed Font Size 默认,如果Label宽度小于文字长度时时,文字大小不自动缩放
minimumScaleFactor 设置最小收缩比例,如果Label宽度小于文字长度时,文字
进行收缩,收缩超过比例后,停止收缩。
minimumFontSize 设置最小收缩字号,如果Label宽度小于文字长度时,文字字号
减小,低于设定字号后,不再减小。//6.0以后不再使用了。
label.minimumScaleFactor = 0.5;
13、adjustsLetterSpacingToFitWidth //改变字母之间的间距来适应Label大小
myLabel.adjustsLetterSpacingToFitWidth = NO;
14、 lineBreakMode //设置文字过长时的显示格式             
label.lineBreakMode = NSLineBreakByCharWrapping;以字符为显示单位显
示,后面部分省略不显示。
label.lineBreakMode = NSLineBreakByClipping;剪切与文本宽度相同的内
容长度,后半部分被删除。
label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字
以……方式省略,显示尾部文字内容。
label.lineBreakMode = NSLineBreakByTruncatingMiddle;中间的内容
以……方式省略,显示头尾的文字内容。
label.lineBreakMode = NSLineBreakByTruncatingTail;结尾部分的内容
以……方式省略,显示头的文字内容。
label.lineBreakMode = NSLineBreakByWordWrapping;以单词为显示单位显
示,后面部分省略不显示。
15、 adjustsFontSizeToFitWidth //设置字体大小适应label宽度  
label.adjustsFontSizeToFitWidth = YES;
16、attributedText:设置标签属性文本。
NSString *text = @"first";
NSMutableAttributedString *textLabelStr =
[[NSMutableAttributedString alloc]
initWithString:text];
[textLabelStr
setAttributes:@{NSForegroundColorAttributeName :
[UIColor lightGrayColor], NSFontAttributeName :
[UIFont systemFontOfSize:17]} range:NSMakeRange(11,
10)];
label.attributedText = textLabelStr;
17、竖排文字显示每个文字加一个换行符,这是最方便和简单的实现方式。
label.text = @"请\n竖\n直\n方\n向\n排\n列";
label.numberOfLines = [label.text length];


18、计算UIlabel 随字体多行后的高度
CGRect bounds = CGRectMake(0, 0, 200, 300);
heightLabel = [myLabel textRectForBounds:bounds
limitedToNumberOfLines:20]; //计算20行后的Label的Frame
NSLog(@"%f",heightLabel.size.height);
19、UILabel根据字数多少自动实现适应高度
UILabel *msgLabel = [[UILabel alloc]
initWithFrame:CGRectMake(15, 45, 0, 0)];
msgLabel.backgroundColor = [UIColor lightTextColor];
[msgLabel setNumberOfLines:0];
msgLabel.lineBreakMode = UILineBreakModeWordWrap;
msgLabel.font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(290, 1000);
msgLabel.text = @"获取到的deviceToken,我们可以通过webservice服务提
交给.net应用程序,这里我简单处理,直接打印出来,拷贝到.net应用环境中使
用。";
CGSize msgSie = [msgLabel.text sizeWithFont:fonts
constrainedToSize:size];
[msgLabel setFrame:CGRectMake(15, 45, 290, msgSie.height)];


20、渐变字体Label
UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage
imageNamed:@"btn.png"]];
NSString *title = @"Setting";
UILabel *titleLabel = [[UILabel alloc]
initWithFrame:CGRectMake(0, 0, 80, 44)];
titleLabel.textColor = titleColor;
titleLabel.text = title;
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:titleLabel];
[titleLabel release];
21、Label添加边框
titleLabel.layer.borderColor = [[UIColor grayColor] CGColor];
titleLabel.layer.borderWidth = 2;
复制代码

 

这段代码动态的创建了一个UILabel,并且把相关常用的属性都列举了.希望对大家有用.

- (void)viewDidLoad {

//创建uilabel

UILabel *label1 = [[UILabel allocinitWithFrame:CGRectMake(204028080)];

//设置背景色

label1.backgroundColor = [UIColor grayColor];

//设置tag

label1.tag = 91;

//设置标签文本

label1.text = @"CCBASE.NET!";

//设置标签文本字体和字体大小

label1.font = [UIFont fontWithName:@"Arial" size:30];

//设置文本对齐方式

label1.textAlignment = UITextAlignmentCenter;

//文本对齐方式有以下三种

//typedef enum {

//    UITextAlignmentLeft = 0,左对齐

//    UITextAlignmentCenter,居中对齐

//    UITextAlignmentRight, 右对齐                 

//} UITextAlignment;

//文本颜色

label1.textColor = [UIColor blueColor];

//超出label边界文字的截取方式

label1.lineBreakMode = UILineBreakModeTailTruncation;

//截取方式有以下6

//typedef enum {       

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

//    UILineBreakModeCharacterWrap,   保留整个字符         

//    UILineBreakModeClip,            到边界为止         

//    UILineBreakModeHeadTruncation,  省略开始,以……代替       

//    UILineBreakModeTailTruncation,  省略结尾,以……代替      

//    UILineBreakModeMiddleTruncation,省略中间,以……代替,多行时作用于最后一行       

//} UILineBreakMode;

//文本文字自适应大小

label1.adjustsFontSizeToFitWidth = YES;

//adjustsFontSizeToFitWidth=YES时候,如果文本font要缩小时

//baselineAdjustment这个值控制文本的基线位置,只有文本行数为1是有效

label1.baselineAdjustment = UIBaselineAdjustmentAlignCenters;

//有三种方式

//typedef enum {

//    UIBaselineAdjustmentAlignBaselines = 0, 默认值文本最上端于label中线对齐

//    UIBaselineAdjustmentAlignCenters,//文本中线于label中线对齐

//    UIBaselineAdjustmentNone,//文本最低端与label中线对齐

//} UIBaselineAdjustment;

//文本最多行数,为0时没有最大行数限制

label1.numberOfLines = 2;

//最小字体,行数为1时有效,默认为0.0

label1.minimumFontSize = 10.0;

//文本高亮

label1.highlighted = YES;

//文本是否可变

label1.enabled = YES;

//去掉label背景色

//label1.backgroundColor = [UIColor clearColor];

//文本阴影颜色

label1.shadowColor = [UIColor grayColor];

//阴影大小

label1.shadowOffset = CGSizeMake(1.01.0);

//是否能与用户交互

label1.userInteractionEnabled = YES;

[self.view addSubview:label1];

[label1 release];

    [super viewDidLoad];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值