UILabel属性详解

来源:互联网 发布:东方网域名 编辑:程序博客网 时间:2024/05/16 19:22
1.text:设置标签显示文本。
2.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;
3.font:设置标签文本字体。
label.font = [UIFont fontWithName:@"Arial" size:16];
4.textColor:设置标签文本颜色。
label.textColor = [UIColor blueColor];
5.textAlignment:设置标签文本对齐方式。
label.textAlignment = NSTextAlignmentCenter;
6.lineBreakMode:设置标签文字过长时的显示方式。
label.lineBreakMode = NSLineBreakByCharWrapping;以字符为显示单位显示,后面部分省略不显示。
label.lineBreakMode = NSLineBreakByClipping;剪切与文本宽度相同的内容长度,后半部分被删除。
label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字以……方式省略,显示尾部文字内容。
label.lineBreakMode = NSLineBreakByTruncatingMiddle;中间的内容以……方式省略,显示头尾的文字内容。
label.lineBreakMode = NSLineBreakByTruncatingTail;结尾部分的内容以……方式省略,显示头的文字内容。
label.lineBreakMode = NSLineBreakByWordWrapping;以单词为显示单位显示,后面部分省略不显示。
7.enabled:设置文字内容是否可变。
8.adjustsFontSizeToFitWidth:文字内容自适应标签宽度。
9.adjustsLetterSpacingToFitWidth:根据字母的间隔自适应标签宽度,超出部分以……显示。
10.numberOfLines:标签最多显示行数。
11.minimumScaleFactor:设置最小字体,与minimumFontSize相同,minimumFontSize在IOS 6后不能使用。
12.highlightedTextColor:设置文本高亮显示颜色,与highlighted一起使用。
13.shadowColor:设置文本阴影颜色。
14.shadowColor:设置文本阴影与原文本的偏移量。label.shadowOffset = CGSizeMake(1.0, 5.0);
15.userInteractionEnabled:设置标签是否忽略或移除用户交互。默认为NO。
16.preferredMaxLayoutWidth:优先选择标签布局的最大宽度。
17.baselineAdjustment:如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为。
label4.baselineAdjustment = UIBaselineAdjustmentNone;
UIBaselineAdjustmentAlignBaselines=0,默认,文本最上端与中线对齐。
UIBaselineAdjustmentAlignCenters,   文本中线与label中线对齐。
UIBaselineAdjustmentNone,  文本最低端与label中线对齐。


一、初始化

1UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 44)];
2     
3[self.view addSubview:myLabel];

二、设置文字

 ①、设置默认文本

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

效果:

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

效果:

关键字标红的效果

01NSString *keyword = @"开源";
02NSString *result = @"开源中国社区";
03 
04// 设置标签文字
05NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:result];
06 
07// 获取标红的位置和长度
08NSRange range = [result rangeOfString:keyword];
09 
10// 设置标签文字的属性
11[attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],   NSFontAttributeName : [UIFont systemFontOfSize:17]} range:range];
12 
13// 显示在Label上
14label.attributedText = attrituteString;

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

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

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

⑥、文字剪裁方式

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

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

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

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

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

       


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

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

③、设置对齐基线

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

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

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

iOS6.0之前:minimumFontSize

iOS6.0之后:minimumScaleFactor

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

⑤、行数

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

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

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

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

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

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