iOS开发之UILabel(富文本)

版权声明:书写博客,不为传道受业,只为更好的梳理更好的记忆,欢迎转载与分享,更多博客请访问:http://blog.csdn.net/myinclude 和 http://www.jianshu.com/u/b20be2dcb0c3 https://blog.csdn.net/myinclude/article/details/52816217

1、常见的属性及说明

NSFontAttributeName  //字体
NSParagraphStyleAttributeName  //段落格式 
NSForegroundColorAttributeName  //字体颜色
NSBackgroundColorAttributeName  //背景颜色
NSStrikethroughStyleAttributeName  //删除线格式
NSUnderlineStyleAttributeName  //下划线格式
NSStrokeColorAttributeName  //删除线颜色
NSStrokeWidthAttributeName  //删除线宽度
NSShadowAttributeName  //阴影

2、常见方法:

//为某一范围内文字设置多个属性
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
//为某一范围内文字添加某个属性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
//为某一范围内文字添加多个属性
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
//移除某范围内的某个属性
- (void)removeAttribute:(NSString *)name range:(NSRange)range;

更多方法和属性说明详见苹果官方说明文档

3、使用示例:

NSString *str = @"犯我中华者,虽远必诛!";
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] 
      initWithString:str];
/*说明:NSAttributedString也能设置,与NSMutableAttributedString的关系类似于NSArray和NSMutableArray*/

(1)、添加字体和设置字体的范围

[attrStr addAttribute:NSFontAttributeName value:
  [UIFont systemFontOfSize:20.0f] range:NSMakeRange(0, 3)];  //字体大小为20.0f
[attrStr addAttribute:NSFontAttributeName value:
  [UIFont boldSystemFontOfSize:20.0f] range:NSMakeRange(0, 3)];  //字体大小为20.0f并且加粗

(2)、添加文字颜色

[attrStr addAttribute:NSForegroundColorAttributeName value:
  [UIColor redColor] range:NSMakeRange(0, 7)];

(3)、添加下划线

[attrStr addAttribute:NSUnderlineStyleAttributeName value:
  [NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, 7)];

(4)、设置段落样式

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
//行间距
paragraph.lineSpacing = 10;
//段落间距
paragraph.paragraphSpacing = 20;
//对齐方式
paragraph.alignment = NSTextAlignmentLeft;
//指定段落开始的缩进像素
paragraph.firstLineHeadIndent = 30;
//调整全部文字的缩进像素paragraph.headIndent = 10;

(5)、添加段落设置

[attrStr addAttribute:NSParagraphStyleAttributeName value:paragraph 
  range:NSMakeRange(0, [str length])];

(6)、添加链接
label添加链接注意:label链接是可以显示出来,但是不能点击,而textView是可以点击的,因为里面有shouldInteractWithURL代理方法回调。

NSString *urlStr = @"www.baidu.com";
NSURL *url = [NSURL URLWithString:urlStr];
[attrStr addAttribute:NSLinkAttributeName value:url range:NSMakeRange(2, 7)];

(7)、一次性搞定:设字号为20,字体颜色为红色

NSDictionary *attDict = [NSDictionary dictionaryWithObjectsAndKeys:
  [UIFont systemFontOfSize:20.0],NSFontAttributeName,
  [UIColor redColor],NSForegroundColorAttributeName,
  nil];
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc] 
  initWithString:@"犯我华夏者,虽远必诛!" attributes:attDict];

4、label其他一些常用属性:

//创建label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 100, 200, 0)];
//设置背景颜色
label.backgroundColor = [UIColor lightGrayColor];
//自动换行
label.numberOfLines = 0;
//设置label的富文本
label.attributedText = attrStr;
//label高度自适应
[label sizeToFit];

//打印高度
CGFloat height = label.frame.size.height;
NSLog(@"height = %f",height);

PS:设置sizeToFit之后是可以取出label的高度的,这样做label高度自适应。但是如果你用第三方框架(如:Masonry)给其加约束,因为约束优先级最高,所以这句会失效

猜你喜欢

转载自blog.csdn.net/myinclude/article/details/52816217