美文网首页
iOS 富文本属性

iOS 富文本属性

作者: coming_168 | 来源:发表于2019-06-06 09:14 被阅读0次

    富文本属性的一些简单使用

    一、NSAttributedString属性列表:

    /** 富文本属性
     NSFontAttributeName                   // 设置字体
     NSForegroundColorAttributeName        // 设置文字颜色
     NSBackgroundColorAttributeName        // 设置背景颜色
     NSKernAttributeName                   // 设置字符间距
     NSParagraphStyleAttributeName         // 设置段落风格
     NSStrikethroughStyleAttributeName     // 添加删除线
     NSStrikethroughColorAttributeName     // 添加删除线颜色
     NSUnderlineStyleAttributeName         // 添加下划线
     NSUnderlineColorAttributeName         // 添加下划线颜色
     NSStrokeColorAttributeName            // 设置文字描边颜色
     NSStrokeWidthAttributeName            // 设置文字描边宽度
     NSShadowAttributeName                 // 设置阴影
     NSLigatureAttributeName               // 设置连体属性
     NSTextEffectAttributeName             // 设置文本特殊效果
     NSAttachmentAttributeName             // 设置文本附件(一般用于图文混排,配合:NSTextAttachment对象使用)
     NSLinkAttributeName                   // 设置链接属性
     NSBaselineOffsetAttributeName         // 设置基线偏移量
     NSObliquenessAttributeName            // 设置字体倾斜
     NSExpansionAttributeName              // 设置文本扁平
     NSWritingDirectionAttributeName       // 设置文字书写方向
     NSVerticalGlyphFormAttributeName      // 设置文本段落排版格式(基本无用,iOS只支持横排)
     */
    

    二、富文本属性设置:

    因富文本属性这些对应的''键''不是那么好找,所以直接封装成一个方法。调用方法比去找对应的键还是要方便一些:对NSMutableAttributedString的分类进行方法扩展。

    .h文件
    @interface NSMutableAttributedString (Category)
    /** 系统字体大小 */
    - (NSMutableAttributedString *)cc_fontSize:(CGFloat)size range:(NSRange)range;
    /** 设置字体 */
    - (NSMutableAttributedString *)cc_font:(UIFont *)font range:(NSRange)range;
    /** 文字颜色 */
    - (NSMutableAttributedString *)cc_textColor:(UIColor *)color range:(NSRange)range;
    /** 背景色 */
    - (NSMutableAttributedString *)cc_backgroundColor:(UIColor *)color range:(NSRange)range;
    /** 添加字间距 */
    - (NSMutableAttributedString *)cc_addSpace:(CGFloat)space range:(NSRange)range;
    /** 仅添加行间距 */
    - (NSMutableAttributedString *)cc_addLineSpace:(CGFloat)space range:(NSRange)range;
    /** 仅添加段落间距 */
    - (NSMutableAttributedString *)cc_addParagraphSpace:(CGFloat)space range:(NSRange)range;
    /** 同时添加行间距和段落间距 */
    - (NSMutableAttributedString *)cc_addLineSpace:(CGFloat)lineSpace ParagraphSpace:(CGFloat)paragraphSpace range:(NSRange)range;
    /** 添加中划线 */
    - (NSMutableAttributedString *)cc_addMidline:(CGFloat)lineHeght range:(NSRange)range;
    /** 中划线颜色 */
    - (NSMutableAttributedString *)cc_midlineColor:(UIColor *)color range:(NSRange)range;
    /** 添加下划线 */
    - (NSMutableAttributedString *)cc_addUnderLine:(NSUnderlineStyle)style range:(NSRange)range;
    /** 下划线颜色 */
    - (NSMutableAttributedString *)cc_underLineColor:(UIColor *)color range:(NSRange)range;
    /** 描边颜色 */
    - (NSMutableAttributedString *)cc_strokeColor:(UIColor *)color range:(NSRange)range;
    /** 给文字添加描边(width:描边框度) */
    - (NSMutableAttributedString *)cc_addStroke:(CGFloat)width range:(NSRange)range;
    /** 添加阴影 */
    - (NSMutableAttributedString *)cc_addShadow:(CGSize)shadowOffset color:(UIColor *)color range:(NSRange)range;
    
    /** 添加图片 */
    - (NSAttributedString *)cc_addImg:(NSString *)imageName frame:(CGRect)frame;
    /** 拼接富文本 */
    - (NSMutableAttributedString *)cc_addAttribute:(NSMutableAttributedString *)attribute;
    
    
    /**
     连字符
     @param num 1:连接,0:无连接
     @param range 范围
     @return 富文本字符串
     */
    - (NSMutableAttributedString *)cc_addLigatureWithNum:(int)num range:(NSRange)range;
    /** 文字效果 */
    - (NSMutableAttributedString *)cc_addEffectWithRange:(NSRange)range;
    /** 链接(在textView中才有用) */
    - (NSMutableAttributedString *)cc_addLink:(NSString *)url range:(NSRange)range;
    /**
     基础偏移量
     @param value 正值向上偏移,负值向下偏移,默认0(不偏移)
     @param range 范围
     @return 富文本字符串
     */
    - (NSMutableAttributedString *)cc_addLineOffset:(CGFloat)value range:(NSRange)range;
    /**
     字体倾斜
     @param value 正值向右倾斜,负值向左倾斜, 默认0(不倾斜)
     @param range 范围
     @return 富文本字符串
     */
    - (NSMutableAttributedString *)cc_addObliqueness:(CGFloat)value range:(NSRange)range;
    /**
     文本扁平化(横向拉伸)
     @param value 正值横向拉伸,负值横向压缩,默认0(不拉伸)
     @param range 范围
     @return 富文本字符串
     */
    - (NSMutableAttributedString *)cc_addExpansion:(CGFloat)value range:(NSRange)range;
    /**
     文字书写方向
     @param value 0,1,2,3(传值"3"的时候文字是从右至左)
     //    NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding
     //    NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding
     //    NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride
     //    NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride
     @param range 范围
     @return 富文本字符串
     */
    - (NSMutableAttributedString *)cc_addWritingDirection:(int)value range:(NSRange)range;
    /**
     文字排版方向
     @param value 0表示横排文本,1表示竖排文本 在iOS中只支持0
     @param range 范围
     @return 富文本字符串
     */
    - (NSMutableAttributedString *)cc_addVerticalGlyph:(CGFloat)value range:(NSRange)range;
    
    @end
    
    @interface NSString (AttStr)
    /** 字符串转富文本 */
    - (NSMutableAttributedString *)cc_strAttribute;
    @end
    
    .m文件
    #import "NSMutableAttributedString+Category.h"
    
    @implementation NSMutableAttributedString (Category)
    
    // 获取字符串范围
    static inline NSRange cc_getRange(NSRange range)
    {
        return NSMakeRange(range.location, range.length);
    }
    
    #pragma mark - 系统字体大小
    - (NSMutableAttributedString *)cc_fontSize:(CGFloat)size range:(NSRange)range{
        [self addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:cc_getRange(range)];
        return self;
    }
    #pragma mark - 设置字体
    - (NSMutableAttributedString *)cc_font:(UIFont *)font range:(NSRange)range{
        [self addAttribute:NSFontAttributeName value:font range:cc_getRange(range)];
        return self;
    }
    
    #pragma mark - 文字颜色
    - (NSMutableAttributedString *)cc_textColor:(UIColor *)color range:(NSRange)range{
        [self addAttribute:NSForegroundColorAttributeName value:color range:cc_getRange(range)];
        return self;
    }
    
    #pragma mark - 背景色
    - (NSMutableAttributedString *)cc_backgroundColor:(UIColor *)color range:(NSRange)range{
        [self addAttribute:NSBackgroundColorAttributeName value:color range:cc_getRange(range)];
        return self;
    }
    
    #pragma mark - 添加字间距
    - (NSMutableAttributedString *)cc_addSpace:(CGFloat)space range:(NSRange)range{
        [self addAttribute:NSKernAttributeName value:@(space) range:cc_getRange(range)];
        return self;
    }
    
    #pragma mark - 仅添加行间距
    - (NSMutableAttributedString *)cc_addLineSpace:(CGFloat)space range:(NSRange)range {
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.lineSpacing = space;  //行间距
        style.lineBreakMode = NSLineBreakByWordWrapping; //换行样式(在什么地方换行)
        [self addAttribute:NSParagraphStyleAttributeName value:style range:cc_getRange(range)];
        return self;
    }
    
    #pragma mark - 仅添加段落间距
    - (NSMutableAttributedString *)cc_addParagraphSpace:(CGFloat)space range:(NSRange)range {
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.paragraphSpacing = space;  //段落间距
        style.lineBreakMode = NSLineBreakByWordWrapping; //换行样式(在什么地方换行)
        [self addAttribute:NSParagraphStyleAttributeName value:style range:cc_getRange(range)];
        return self;
    }
    
    #pragma mark - 同时添加行间距和段落间距
    - (NSMutableAttributedString *)cc_addLineSpace:(CGFloat)lineSpace ParagraphSpace:(CGFloat)paragraphSpace range:(NSRange)range {
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.lineSpacing = lineSpace;  //行间距
        style.paragraphSpacing = paragraphSpace;  //段落间距
        style.lineBreakMode = NSLineBreakByClipping; //换行样式(在什么地方换行)
        [self addAttribute:NSParagraphStyleAttributeName value:style range:cc_getRange(range)];
        return self;
    }
    
    #pragma mark - 添加中划线
    - (NSMutableAttributedString *)cc_addMidline:(CGFloat)lineHeght range:(NSRange)range {
        [self addAttribute:NSStrikethroughStyleAttributeName value:@(lineHeght) range:cc_getRange(range)];
        return self;
    }
    
    #pragma mark - 中划线颜色
    - (NSMutableAttributedString *)cc_midlineColor:(UIColor *)color range:(NSRange)range {
        [self addAttribute:NSStrikethroughColorAttributeName value:color range:cc_getRange(range)];
        return self;
    }
    
    #pragma mark - 添加下划线
    - (NSMutableAttributedString *)cc_addUnderLine:(NSUnderlineStyle)style range:(NSRange)range{
        [self addAttribute:NSUnderlineStyleAttributeName value:@(style) range:cc_getRange(range)];
        return self;
    }
    
    #pragma mark - 下划线颜色
    - (NSMutableAttributedString *)cc_underLineColor:(UIColor *)color range:(NSRange)range{
        [self addAttribute:NSUnderlineColorAttributeName value:color range:cc_getRange(range)];
        return self;
    }
    
    #pragma mark - 描边颜色
    - (NSMutableAttributedString *)cc_strokeColor:(UIColor *)color range:(NSRange)range{
        [self addAttribute:NSStrokeColorAttributeName value:color range:cc_getRange(range)];
        return self;
    }
    
    #pragma mark - 给文字添加描边(width:描边框度)
    - (NSMutableAttributedString *)cc_addStroke:(CGFloat)width range:(NSRange)range{
        [self addAttribute:NSStrokeWidthAttributeName value:@(width) range:cc_getRange(range)];
        return self;
    }
    
    #pragma mark - 添加阴影
    - (NSMutableAttributedString *)cc_addShadow:(CGSize)shadowOffset color:(UIColor *)color range:(NSRange)range{
        NSShadow *shadow = [[NSShadow alloc] init];
        shadow.shadowColor = color;
        shadow.shadowOffset = shadowOffset;
        [self addAttribute:NSShadowAttributeName value:shadow range:cc_getRange(range)];
        return self;
    }
    
    #pragma mark - 添加图片(返回不可变的属性)
    - (NSAttributedString *)cc_addImg:(NSString *)imageName frame:(CGRect)frame{
        UIImage *smileImage = [UIImage imageNamed:imageName];
        NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
        textAttachment.image = smileImage;
        textAttachment.bounds = frame; 
        return [NSAttributedString attributedStringWithAttachment:textAttachment];
    }
    
    #pragma mark - 拼接富文本
    - (NSMutableAttributedString *)cc_addAttribute:(NSMutableAttributedString *)attribute{
        [self appendAttributedString:attribute];
        return self;
    }
    
    #pragma mark - 连字符
    - (NSMutableAttributedString *)cc_addLigatureWithNum:(int)num range:(NSRange)range {
        [self addAttribute:NSLigatureAttributeName value:[NSNumber numberWithInt:num] range:cc_getRange(range)];
        return self;
    }
    #pragma mark - 文字效果
    - (NSMutableAttributedString *)cc_addEffectWithRange:(NSRange)range {
        [self addAttribute:NSTextEffectAttributeName value:NSTextEffectLetterpressStyle range:cc_getRange(range)];
        return self;
    }
    #pragma mark - 链接
    - (NSMutableAttributedString *)cc_addLink:(NSString *)url range:(NSRange)range {
        [self addAttribute:NSLinkAttributeName value:[NSURL URLWithString:url] range:cc_getRange(range)];
        return self;
    }
    #pragma mark - 基础偏移量
    - (NSMutableAttributedString *)cc_addLineOffset:(CGFloat)value range:(NSRange)range {
        [self addAttribute:NSBaselineOffsetAttributeName value:@(value) range:cc_getRange(range)];
        return self;
    }
    #pragma mark - 字体倾斜
    - (NSMutableAttributedString *)cc_addObliqueness:(CGFloat)value range:(NSRange)range {
        [self addAttribute:NSObliquenessAttributeName value:@(value) range:cc_getRange(range)];
        return self;
    }
    #pragma mark - 文本扁平化(横向拉伸)
    - (NSMutableAttributedString *)cc_addExpansion:(CGFloat)value range:(NSRange)range {
        [self addAttribute:NSExpansionAttributeName value:@(value) range:cc_getRange(range)];
        return self;
    }
    #pragma mark - 文字书写方向
    - (NSMutableAttributedString *)cc_addWritingDirection:(int)value range:(NSRange)range {
        [self addAttribute:NSWritingDirectionAttributeName value:@[@(value)] range:cc_getRange(range)];
        return self;
    }
    #pragma mark - 文字排版方向
    - (NSMutableAttributedString *)cc_addVerticalGlyph:(CGFloat)value range:(NSRange)range {
        [self addAttribute:NSVerticalGlyphFormAttributeName value:@(value) range:cc_getRange(range)];
        return self;
    }
    
    @end
    
    // NSString (AttStr)
    @implementation NSString (AttStr)
    #pragma mark - 创建字符串转富文本
    - (NSMutableAttributedString *)cc_strAttribute{
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:self];
        return attStr;
    }
    @end
    

    三、使用方法:

    • 1⃣️创建富文本:

    NSMutableAttributedString *attStr = @"我是一段测试文字,没什么鸟用的文字。\n我是下一行的文字,测试段落效果,对齐方式很随便。\n十有九人堪白眼,百无—用是书生。莫因诗卷愁成谶,春鸟秋虫自作声。".cc_strAttribute;
    
    • 2⃣️添加富文本属性:

    1. NSFontAttributeName:设置字体
    [attStr cc_fontSize:26 range:NSMakeRange(9, 9)];
    
    效果如图: image.png
    2. NSForegroundColorAttributeName:设置文字颜色
    [attStr cc_textColor:UIColor.redColor range:NSMakeRange(9, 9)];
    
    效果如图: image.png
    3. NSBackgroundColorAttributeName:设置背景颜色
    [attStr cc_backgroundColor:UIColor.orangeColor range:NSMakeRange(19, 9)];
    
    效果如图: image.png
    4. NSKernAttributeName:设置字符间距
    [attStr cc_addSpace:10 range:NSMakeRange(19, 9)];
    
    效果如图: image.png
    5. NSParagraphStyleAttributeName:设置段落风格
    // 行间距20,段落间距50,范围range(0,9)
    [attStr cc_addLineSpace:20 ParagraphSpace:50 range:NSMakeRange(0, 9)];
    
    效果如图: image.png
    6. NSStrikethroughStyleAttributeName:添加删除线
    [attStr cc_addMidline:2 range:NSMakeRange(9, 9)];
    
    效果如图: image.png
    7. NSStrikethroughColorAttributeName:添加删除线颜色
        [attStr cc_addMidline:2 range:NSMakeRange(9, 9)];
        [attStr cc_midlineColor:UIColor.redColor range:NSMakeRange(9, 9)];
    
    效果如图: image.png
    8. NSUnderlineStyleAttributeName:添加下划线
    [attStr cc_addUnderLine:NSUnderlineStyleSingle range:NSMakeRange(attStr.length - 32, 32)];
    
    效果如图: image.png
    9. NSUnderlineColorAttributeName:添加下划线颜色
        [attStr cc_addUnderLine:NSUnderlineStyleSingle range:NSMakeRange(attStr.length - 32, 32)];
        [attStr cc_underLineColor:UIColor.redColor range:NSMakeRange(attStr.length - 32, 32)];
    
    效果如图: image.png
    10. NSStrokeWidthAttributeName:设置文字描边宽度
    [attStr cc_addStroke:2 range:NSMakeRange(9, 9)];
    
    效果如图: image.png
    11. NSStrokeColorAttributeName:设置文字描边颜色
        [attStr cc_addStroke:2 range:NSMakeRange(9, 9)];
        [attStr cc_strokeColor:UIColor.blueColor range:NSMakeRange(9, 9)];
    
    
    效果如图: image.png
    12. NSShadowAttributeName:设置阴影
    [attStr cc_addShadow:CGSizeMake(2, 2) color:UIColor.redColor range:NSMakeRange(19, 9)];
    
    效果如图: image.png
    13. NSLigatureAttributeName:设置连体属性
        // 1:连接,0:无连接。看不出有什么效果
        NSMutableAttributedString *attStr1 = @"abcdefg。\nABCDEFG。\n十有九人堪白眼,百无—用是书生。莫因诗卷愁成谶,春鸟秋虫自作声。".cc_strAttribute;
        [attStr cc_addLigatureWithNum:1 range:NSMakeRange(0, 8)];
        [attStr cc_addLigatureWithNum:0 range:NSMakeRange(9, 8)];
    
    效果如图: image.png
    14. NSTextEffectAttributeName:设置文本特殊效果
    [attStr cc_addEffectWithRange:NSMakeRange(9, 9)];
    
    效果如图: image.png
    15. NSAttachmentAttributeName:设置文本附件(一般用于图文混排,配合:NSTextAttachment对象使用)
        NSAttributedString *imgStr = [@"".cc_strAttribute cc_addImg:@"tips_img" frame:CGRectMake(0, -4, 22, 22)];
        [attStr appendAttributedString:imgStr];
        [attStr insertAttributedString:imgStr atIndex:28];
    
    效果如图: image.png
    16. NSLinkAttributeName:设置链接属性(要在textView中才有效)
        UITextView *textView = [[UITextView alloc] init];
        textView.frame = CGRectMake(100, 300, 200, 200);
        [self.view addSubview:textView];
        textView.delegate = self;
        // 要禁用textView的编辑状态,点击跳转才有效
        textView.editable = NO;
        //  传入点击跳转的url
        [attStr cc_addLink:@"https://www.jianshu.com/p/d36844ea9e27" range:NSMakeRange(0, attStr.length)];
        self.textView.attributedText = attStr;
    
    效果如图: image.png
    17. NSBaselineOffsetAttributeName:设置基线偏移量
        // offset为正 -> 向上偏移
        [attStr cc_addLineOffset:15 range:NSMakeRange(2, 2)];
        [attStr cc_textColor:UIColor.redColor range:NSMakeRange(2, 2)];
        // offset为负 -> 向下偏移
        [attStr cc_addLineOffset:-15 range:NSMakeRange(6, 2)];
        [attStr cc_textColor:UIColor.redColor range:NSMakeRange(6, 2)];
    
    效果如图: image.png
    18. NSObliquenessAttributeName:设置字体倾斜
        // 正值向右倾斜,负值向左倾斜, 默认0(不倾斜)
        [attStr cc_addObliqueness:1 range:NSMakeRange(9, 9)];
        [attStr cc_textColor:UIColor.redColor range:NSMakeRange(9, 9)];
        [attStr cc_addObliqueness:-1 range:NSMakeRange(19, 9)];
        [attStr cc_textColor:UIColor.greenColor range:NSMakeRange(19, 9)];
    
    效果如图: image.png
    19. NSExpansionAttributeName:设置文本扁平
        // 正值横向拉伸,负值横向压缩,默认0(不拉伸)
        [attStr cc_addExpansion:1 range:NSMakeRange(9, 9)];
        [attStr cc_textColor:UIColor.redColor range:NSMakeRange(9, 9)];
        [attStr cc_addExpansion:-1 range:NSMakeRange(19, 9)];
        [attStr cc_textColor:UIColor.greenColor range:NSMakeRange(19, 9)];
    
    效果如图: image.png
    20. NSWritingDirectionAttributeName:设置文字书写方向
        // 传值"3"的时候文字是:右->左,其他值:左->右
        [attStr cc_addWritingDirection:3 range:NSMakeRange(0, 18)];
        [attStr cc_textColor:UIColor.redColor range:NSMakeRange(0, 18)];
    
    效果如图: image.png
    21. NSVerticalGlyphFormAttributeName:设置文本段落排版格式(基本无用,iOS只支持横排)
        // 0表示横排文本,1表示竖排文本;在iOS中只支持0,设置1无效
        [attStr cc_addVerticalGlyph:0 range:NSMakeRange(0, attStr.length)];
        [attStr cc_textColor:UIColor.redColor range:NSMakeRange(0, attStr.length)];
    
    效果如图: image.png
    • 3⃣️富文本属性赋值
        self.label.attributedText = attStr;
    //    self.textView.attributedText = attStr;
    

    相关文章

      网友评论

          本文标题:iOS 富文本属性

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