NSAttributedString - 属性字符串(富文本)

1 Foundation 中有属性字符串
2 UIKit 中对属性字符串定义一些常用的类型

NSAttributedString 一般创建

一般创建 通过 NSMutableAttributedString 添加各种属性,虽然代码比较多,但是跟丰富一点。

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"asdfghjkl"];
    
    [attributedString addAttribute:NSUnderlineStyleAttributeName//下划线
                             value:[NSNumber numberWithInt:NSUnderlineStyleThick]//类型。也可以数值 表示粗细
                             range:NSMakeRange(8, 8)];

    [attributedString addAttribute:NSUnderlineColorAttributeName//下划线。颜色
                             value:[UIColor redColor]
                             range:NSMakeRange(8, 8)];

// 类似的 AttributeteName 还有很多,对同一个NSMutableAttributedString 可以添加多个属性,具体下面解释。
NSFontAttributeName 字体 UIFont
NSForegroundColorAttributeName 字体颜色 UIColor
NSBackgroundColorAttributeName 字背景颜色 UIColor
NSUnderlineStyleAttributeName 下划线粗细,类型 NSNumber
NSUnderlineColorAttributeName 下划线颜色 UIColor
NSStrikethroughStyleAttributeName 删除线粗细,类型 NSNumber
NSStrikethroughColorAttributeName 删除线颜色 UIColor
NSBaselineOffsetAttributeName 上下偏移 NSNumber
NSObliquenessAttributeName 倾斜 NSNumber
NSExpansionAttributeName 横向拉伸压缩 NSNumber
NSStrokeWidthAttributeName 描边宽度 NSNumber
NSStrokeColorAttributeName 描边颜色 UIColor
NSShadowAttributeName 字符阴影 NSShadow 
NSLinkAttributeName 网址链接 NSURL
NSParagraphStyleAttributeName 段落 NSParagraphStyle
以上还不够用的话,研究更多下面的把。

未知
NSLigatureAttributeName
NSKernAttributeName
NSTextEffectAttributeName NSTextEffectLetterpressStyle
NSAttachmentAttributeName
NSWritingDirectionAttributeName
NSVerticalGlyphFormAttributeName


还有更多 文件的富文本格式 0.0 NSAttributedStringDocumentFormats

NSAttributedString 快速创建

与一般创建类似,只是把各种属性,以key - value 形式揉成字典,NSAttributedString创建成字符串,

    UILabel *testLabel = [UILabel new];
    NSDictionary *paramas = @{NSUnderlineStyleAttributeName:@(2),
                              NSUnderlineColorAttributeName:[UIColor redColor]
                              };
    NSAttributedString *att = [[NSAttributedString alloc] initWithString:@"testaaa" attributes:paramas];    

Foundation 中的 一些属性字符串操作

NSAttributedString 部分

// 截取
    NSAttributedString *att2 = [att1 attributedSubstringFromRange:NSMakeRange(0, 2)];

// 比较
[att1 isEqualToAttributedString att2];

// 遍历 某种属性的字符串 位置
    [attributedString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, 3) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
        NSLog(@"- - - - - -%zi",range.location); 
    }];


NSMutableAttributedString 部分
// 有更多的类似字符串的 替换,插入,删除的操作,不写了

UIKit 拓展操作

NSAttributedStringDocumentFormats 水好深。。

1

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文所涉及的代码你可以在这里下载到https://github.com/kejinlu/CTTest,包含两个项目...
    eb99d15a673d阅读 1,281评论 0 6
  • 与NSString类似,在iOS中AttributedString也分为NSAttributedString和 N...
    钱十六阅读 794评论 0 0
  • 内容来自于 iOS文档中 About Text Handling in iOS 部分 ios平台提供了显示及编辑文...
    纵横而乐阅读 6,876评论 2 21
  • 我有一只红色的行李箱 我曾拖着它去流浪 火红的颜色如同太阳 如今它却蒙满灰尘 远方的人在路上 我的脚下有石子
    藜九阅读 208评论 2 0
  • 此刻,我坐了下来,在一个小板凳上。我不知道是谁把我叫到这里来的,我发现天已经黑了,周围还有许多人也和我一样的坐着,...
    叮咚的你阅读 554评论 0 3