美文网首页iOS开发
iOS-NSAttributedString属性字符串

iOS-NSAttributedString属性字符串

作者: Imkata | 来源:发表于2021-04-18 16:18 被阅读0次

    iOS属性字符串的API也不多,但是没有系统的整理过,现整理如下:

    NSAttributedString所有API

    //定义NSAttributedStringKey,其实就是字符串类型
    typedef NSString * NSAttributedStringKey;
    //获取属性字符串对应的普通字符串
    @property (readonly, copy) NSString *string;
    //属性字符串的长度
    @property (readonly) NSUInteger length;
    
    //获取range范围内,location位置开始的所有属性字符串,返回值是字典,字典的key是NSAttributedStringKey,字典的value是对应的属性对象
    //range一般传NULL,代表所有范围,location一般从0开始
    - (NSDictionary<NSAttributedStringKey, id> *)attributesAtIndex:(NSUInteger)location effectiveRange:(nullable NSRangePointer)range;
    //获取属性名为attrName的对应的属性对象
    - (nullable id)attribute:(NSAttributedStringKey)attrName atIndex:(NSUInteger)location effectiveRange:(nullable NSRangePointer)range;
    
    //获取range范围内对应的属性字符串
    - (NSAttributedString *)attributedSubstringFromRange:(NSRange)range;
    
    //获取属性字典
    - (NSDictionary<NSAttributedStringKey, id> *)attributesAtIndex:(NSUInteger)location longestEffectiveRange:(nullable NSRangePointer)range inRange:(NSRange)rangeLimit;
    //获取attrName对应的属性对象
    - (nullable id)attribute:(NSAttributedStringKey)attrName atIndex:(NSUInteger)location longestEffectiveRange:(nullable NSRangePointer)range inRange:(NSRange)rangeLimit;
    
    //判断两个属性字符串是否相等
    - (BOOL)isEqualToAttributedString:(NSAttributedString *)other;
    
    //初始化属性字符串
    - (instancetype)initWithString:(NSString *)str;
    - (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attrs;
    - (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;
    
    typedef NS_OPTIONS(NSUInteger, NSAttributedStringEnumerationOptions) {
      NSAttributedStringEnumerationReverse = (1UL << 1),
      NSAttributedStringEnumerationLongestEffectiveRangeNotRequired = (1UL << 20)
    };
    
    //遍历enumerationRange范围内的属性字符串,结果是字典,结果在回调block里面
    - (void)enumerateAttributesInRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(NSDictionary<NSAttributedStringKey, id> *attrs, NSRange range, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
    //遍历enumerationRange范围内,属性名attrName对应的属性对象,结果在回调block里面
    - (void)enumerateAttribute:(NSAttributedStringKey)attrName inRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(id _Nullable value, NSRange range, BOOL *stop))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
    

    NSMutableAttributedString所有API

    //获取属性字符串对应的普通字符串
    @property (readonly, retain) NSMutableString *mutableString;
    
    //将range内的东西替换为str
    - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;
    //设置range范围内对应的属性字典
    - (void)setAttributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attrs range:(NSRange)range;
    
    //添加range范围内,属性名为name对应的属性对象
    - (void)addAttribute:(NSAttributedStringKey)name value:(id)value range:(NSRange)range;
    //移除range范围内,属性名为name对应的属性对象
    - (void)removeAttribute:(NSAttributedStringKey)name range:(NSRange)range;
    //添加range范围内的属性字典
    - (void)addAttributes:(NSDictionary<NSAttributedStringKey, id> *)attrs range:(NSRange)range;
    

    对于addAttribute方法,attrName可以是NSAttributedStringKey,也可以是其他自定义的字符串作为key,当然获取的时候也要使用这个key才可以获取到对应的属性对象,如下:

    [attributedText addAttribute:@"specials" value:specials range:NSMakeRange(0, 1)];
    NSArray *specials = [self.attributedText attribute:@"specials" atIndex:0 effectiveRange:NULL];
    
    //将range内的东西替换为attrString
    - (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString;
    //插入attrString到loc的位置
    - (void)insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc;
    //后面拼接attrString
    - (void)appendAttributedString:(NSAttributedString *)attrString;
    //删除range范围内的东西
    - (void)deleteCharactersInRange:(NSRange)range;
    //设置attrString
    - (void)setAttributedString:(NSAttributedString *)attrString;
    
    //开始编辑属性字符串调用
    - (void)beginEditing;
    //结束编辑属性字符串调用
    - (void)endEditing;
    

    NSAttributedStringKey

    NSFontAttributeName:字体大小,取值为UIFont
    NSForegroundColorAttributeName,字体颜色,取值为UIColor
    NSBackgroundColorAttributeName,背景颜色,取值为UIColor
    NSStrikethroughStyleAttributeName 设置删除线,取值为NSUnderlineStyle,具体值看下面枚举
    NSStrikethroughColorAttributeName 设置删除线颜色,取值为UIColor
    NSUnderlineStyleAttributeName 设置下划线,取值为NSUnderlineStyle,具体值看下面枚举
    NSUnderlineColorAttributeName 设置下划线颜色,取值为UIColor
    NSParagraphStyleAttributeName,设置文本段落排版格式,取值为 NSMutableParagraphStyle 对象(详情见下面的API说明)
    NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
    NSLinkAttributeName 设置链接属性,取值为NSURL对象,点击后调用浏览器打开指定URL地址
    
    NSStrokeWidthAttributeName  设置笔画宽度(粗细),取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
    NSStrokeColorAttributeName  设置笔画填充部分颜色,不是字体颜色,取值为 UIColor 对象
    NSShadowAttributeName 设置阴影属性,取值为 NSShadow 对象
    NSTextEffectAttributeName 设置印刷效果,取值为 NSTextEffectStyle,目前只有NSTextEffectLetterpressStyle(图版印刷效果)可用
    NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
    NSKernAttributeName  设置字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
    NSLigatureAttributeName  设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
    NSObliquenessAttributeName  设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
    NSExpansionAttributeName 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
    NSWritingDirectionAttributeName 设置文字书写方向,取值为NSWritingDirection
    NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为NSTextLayoutOrientation,默认横向排版
    

    NSMutableParagraphStyle

    文本段落排版格式,对象属性如下:

    CGFloat lineSpacing; 字体的行间距
    CGFloat paragraphSpacing; 段与段之间的间距 
    NSTextAlignment alignment; 对齐方式,具体值看下面枚举
    CGFloat firstLineHeadIndent; 首行缩进多少
    CGFloat headIndent; 头部缩进多少(首行除外) 
    CGFloat tailIndent; 尾部缩进多少
    NSLineBreakMode lineBreakMode; 换行模式,具体值看下面枚举
    CGFloat minimumLineHeight; 最小行高
    CGFloat maximumLineHeight; 最大行高
    NSWritingDirection baseWritingDirection; 书写方向,具体值看下面枚举
    CGFloat lineHeightMultiple; 行高因子,在受最小和最大行高限制之前,将自然行高乘以这个因子(如果是正数)。
    CGFloat paragraphSpacingBefore; 上一段的底端(或段末的空格,如果有的话)到本段的顶部的距离。
    float hyphenationFactor; 连字符属性,取值 0 到 1 之间,可以断词
    

    其他枚举

    设置文字对齐方向:

    typedef NS_ENUM(NSInteger, NSTextAlignment) {
        NSTextAlignmentLeft      = 0,    // 默认左对齐
        NSTextAlignmentCenter    = 1,    // 居中对齐
        NSTextAlignmentRight     = 2,    // 右对齐
        NSTextAlignmentJustified = 3,    // 两端对齐。段落的最后一行是自然对齐的。
        NSTextAlignmentNatural   = 4     // 指示脚本的默认对齐方式
    };
    

    设置换行:

    typedef NS_ENUM(NSInteger, NSLineBreakMode) {
        NSLineBreakByWordWrapping = 0,  // 默认单词换行,第一行末尾和第二行末尾都保留完整的单词
        NSLineBreakByClipping,          // 两行显示UILabel能显示多少就显示多少,第一行末尾最后一个单词没有被截断而是全部显示到第一行,第二行末尾是按字符裁剪,导致单词typically只剩下typic
        NSLineBreakByCharWrapping,      // 字符换行,与Clip的区别在第一行,将最后一个单词截断了
        NSLineBreakByTruncatingHead,    // 头部省略,第一行末尾是完整单词,第二行最前面三个点来表示省略内容 "...wxyz"。
        NSLineBreakByTruncatingMiddle,   // 中间省略,第一行末尾是完整单词,第二行中间三个点来表示省略内容 "abcd..."
        NSLineBreakByTruncatingTail      // 尾部省略,第一行末尾是完整单词,第二行尾部三个点来表示省略内容 "ab...yz"
    };
    

    设置书写方向:

    typedef NS_ENUM(NSInteger, NSWritingDirection) {
        NSWritingDirectionNatural       = -1,   // 使用Unicode Bidi算法规则P2和P3确定方向
        NSWritingDirectionLeftToRight   = 0,    // 从左到右书写
        NSWritingDirectionRightToLeft   = 1     // 从右到左书写
    };
    

    设置横线样式:

    typedef NS_OPTIONS(NSInteger, NSUnderlineStyle) {
        NSUnderlineStyleNone 无横线
        NSUnderlineStyleSingle 细单横线
        NSUnderlineStyleDouble 细双横线
        NSUnderlineStyleThick 厚单横线
        NSUnderlinePatternSolid 连续的实线
        NSUnderlinePatternDot 点,比如这样:------
        NSUnderlinePatterDash 破折号,比如这样:—— —— ——
        NSUnderlinePatternDashDot 连续的破折号和点,比如这样:——-——-——-
        NSUnderlinePatternDashDotDot 破折号、点、点,比如:——--——--——--
        NSUnderlineByWord 在有空格的地方不设置下划线/删除线
    };
    

    设置文字排版方向:

    typedef NS_ENUM(NSInteger, NSTextLayoutOrientation) {
        NSTextLayoutOrientationHorizontal = 0, // 横向排版
        NSTextLayoutOrientationVertical = 1, // 竖向排版
    }
    

    相关文章

      网友评论

        本文标题:iOS-NSAttributedString属性字符串

        本文链接:https://www.haomeiwen.com/subject/nngmlltx.html