7_iOS干货6_iOS 的富文本属性NSAttributedString汇总

1,NSAttributedString属性汇总

 

 

 

2,段落属性汇总

NSParagraphStyleAttributeName:值为NSParagraphStyle,设置段落属性,默认值为[NSParagraphStyle defaultParagraphStyle]返回的值。NSMutableParagraphStyle与NSParagraphStyle包括一下属性

 

3,设置富文本

获取富文本的四种方式
方式1:
     //初始化富文本对象:
    NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:@"1,我们都有一个家名字叫中国"];
    //给富文本添加属性1-字体大小
    [attributedStr addAttribute:NSFontAttributeName
                          value:[UIFont systemFontOfSize:16.0]
                          range:NSMakeRange(2, 2)];
    //给富文本添加属性1-字间距
    [attributedStr addAttribute:NSKernAttributeName
                          value:@10
                          range:NSMakeRange(2, 2)];

方式2:
//初始化富文本对象
    NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:@"2,我们都有一个家名字叫中国"];
    //富文本的属性通过字典的形式传入
    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)];

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

方法4:
NSString * htmlString = @"<html><body> <font size=\"8\" color=\"blue\">4,我们都有一个家名字叫</a> <a></font> <font size=\"13\" color=\"red\">中国!</font></a> </body></html>";
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

4,获取富文本

//获取label的富文本属性attributedText
NSRange rang3 = NSMakeRange(0, label3.text.length);
[label3.attributedText enumerateAttribute:NSParagraphStyleAttributeName inRange:rang3 options:NSAttributedStringEnumerationReverse usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
        NSMutableParagraphStyle *style = (NSMutableParagraphStyle *)value;
        NSLog(@"lineSpace是:%f",style.lineSpacing);
        
    }];

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值