iOS 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了

 

copy from http://my.oschina.net/joanfen/blog/145184

转载于:https://www.cnblogs.com/wangyang1213/p/5319259.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值