Label--关于Label富文本

来源:互联网 发布:基于单片机的毕业设计 编辑:程序博客网 时间:2024/05/17 01:22

一、label的富文本属性

label.attributedText

需要注意一点:如果一个label设置了富文本这个属性,那它其他的设置都将失效。

二、富文本对象的创建

//初始化富文本对象的方法一:    - (id)initWithString:(NSString *)str;

//初始化富文本对象的同时设置富文本对象的属性    - (id)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;

三、设置Label的富文本对象

方法一:创建富文本对象后逐个添加富文本对象的属性

    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 300, 80)];    label.backgroundColor = [UIColor lightGrayColor];    //初始化富文本对象:    NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:@"我们都有一个家名字叫中国"];    //给富文本添加属性1-字体大小    [attributedStr addAttribute:NSFontAttributeName                          value:[UIFont systemFontOfSize:16.0]                          range:NSMakeRange(2, 2)];    //给富文本添加属性2-字体颜色    [attributedStr addAttribute:NSForegroundColorAttributeName                          value:[UIColor redColor]                          range:NSMakeRange(2, 2)];    //给富文本添加属性3-下划线    [attributedStr addAttribute:NSUnderlineStyleAttributeName                          value:@(NSUnderlineStyleSingle)                          range:NSMakeRange(2, 2)];    //设置label的富文本属性    label.attributedText = attributedStr;    [self.view addSubview:label];
方法二:创建富文本对象后统一设置富文本对象的属性

    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 300, 80)];    label.backgroundColor = [UIColor lightGrayColor];    //初始化富文本对象    NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:@"我们都有一个家名字叫中国"];    //富文本的属性通过字典的形式传入    NSDictionary *attributeDict = [NSDictionary dictionaryWithObjectsAndKeys:                                   [UIFont systemFontOfSize:16.0],NSFontAttributeName,//字体                                   [UIColor redColor],NSForegroundColorAttributeName,//字体颜色                                   [UIColor greenColor],NSBackgroundColorAttributeName,//字体背景色                                   @(NSUnderlineStyleSingle),NSUnderlineStyleAttributeName,//下划线                                   @(NSUnderlineStyleSingle),NSStrikethroughStyleAttributeName,//删除线                                   [UIColor blueColor],NSUnderlineColorAttributeName,//下划线颜色                                   [UIColor yellowColor],NSStrikethroughColorAttributeName,//删除线颜色                                   [NSURL URLWithString:@"http://www.baidu.com"],NSLinkAttributeName,                                   nil];    //统一设置富文本对象的属性    [attributedStr addAttributes:attributeDict range:NSMakeRange(2, 2)];    //或者也可以使用下面的方法    [attributedStr setAttributes:attributeDict range:NSMakeRange(4, 2)];    //设置label的富文本属性    label.attributedText = attributedStr;    [self.view addSubview:label];

方法三:创建富文本对象的同时就添加属性

    //富文本的属性通过字典的形式传入    NSDictionary *attributeDict = [NSDictionary dictionaryWithObjectsAndKeys:                                   [UIFont systemFontOfSize:16.0],NSFontAttributeName,//字体大小                                   [UIColor redColor],NSForegroundColorAttributeName,//字体颜色                                   @(NSUnderlineStyleSingle),NSUnderlineStyleAttributeName,//下划线                                   nil];    //初始化富文本对象的同时设置富文本对象的属性    NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc]initWithString:@"我们都有一个家名字叫中国" attributes:attributeDict];    //设置label的富文本属性    label.attributedText = attributedStr;    [self.view addSubview:label];

PS:同理一般有add方法就会有remove方法,下面是富文本对象的其它方法

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;//添加一个属性- (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range;//添加一组属性- (void)removeAttribute:(NSString *)name range:(NSRange)range;//移除一个属性


四、常见的属性及说明

NSFontAttributeName

字体

NSParagraphStyleAttributeName

段落格式

NSForegroundColorAttributeName

字体颜色

NSBackgroundColorAttributeName

背景颜色

NSStrikethroughStyleAttributeName

删除线格式

NSUnderlineStyleAttributeName

下划线格式

NSStrokeColorAttributeName

删除线颜色

NSStrokeWidthAttributeName

删除线宽度

NSShadowAttributeName

阴影


参考文章:

http://www.tuicool.com/articles/QZ3If2

http://my.oschina.net/u/2340880/blog/397500

http://www.itstrike.cn/Question/c688711b-0eba-465f-9880-35076f5ba1c2.html//关于link富文本 textView

http://www.bubuko.com/infodetail-382485.html









0 0
原创粉丝点击