IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    简易封装NSMutableAttributedString类

    键盘上的舞者发表于 2016-10-22 15:13:44
    love 0

    在IOS开发中,NSMutableAttributeString这个类也是经常使用到的。

    – (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

    使用这个方法给一个NSMutableAttributeString添加一些富文本属性,但是感觉使用起来略微有点麻烦,那么我们就来对他进行一下封装。(下载地址)

    来说一下我是如何封装的^_^

    首先,我们新建一个协议Protocol

    //
    //  RDStringAttributeProtocol.h
    //  POP动画
    //
    //  Created by 键盘上的舞者 on 10/19/16.
    //  Copyright © 2016 键盘上的舞者. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @protocol RDStringAttributeProtocol <NSObject>
    
    @required
    /**
     *  属性名
     *
     *  @return 属性名
     */
    - (NSString *)attributeName;
    /**
     *  属性值
     *
     *  @return 属性值
     */
    - (id)attributeValue;
    
    @optional
    
    - (NSRange)effectiveStringRange;
    
    @end

    然后新建一个类作为基类继承自NSObject,并遵循上面的Protocol

    //
    //  RDStringAttribute.h
    //  POP动画
    //
    //  Created by 键盘上的舞者 on 10/19/16.
    //  Copyright © 2016 键盘上的舞者. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    #import "RDStringAttributeProtocol.h"
    
    @interface RDStringAttribute : NSObject <RDStringAttributeProtocol>
    
    /**
     *  属性生效范围
     */
    @property (nonatomic, assign) NSRange effectiveRange;
    
    @end

    //
    //  RDStringAttribute.m
    //  POP动画
    //
    //  Created by 键盘上的舞者 on 10/19/16.
    //  Copyright © 2016 键盘上的舞者. All rights reserved.
    //
    
    #import "RDStringAttribute.h"
    
    @implementation RDStringAttribute
    
    - (NSString *)attributeName {
        return nil;
    }
    
    - (id)attributeValue {
        return nil;
    }
    
    - (NSRange)effectiveStringRange {
        return self.effectiveRange;
    }
    
    @end

    一般来说,我们使用到的富文本属性无非就是两个,NSFontAttributeName和NSForegroundColorAttributeName。更多的属性你可以查看NSAttributeString.h这个头文件。(command+shift+o去搜索这个头文件)

    那么我们新建两个类,RDFontAttribute和RDForegroundAttribute继承自RDStringAttribute。

    //
    //  RDFontAttribute.h
    //  POP动画
    //
    //  Created by 键盘上的舞者 on 10/19/16.
    //  Copyright © 2016 键盘上的舞者. All rights reserved.
    //
    
    #import "RDStringAttribute.h"
    
    @interface RDFontAttribute : RDStringAttribute
    
    @property (nonatomic, strong) UIFont *font;
    
    @end

    //
    //  RDFontAttribute.m
    //  POP动画
    //
    //  Created by 键盘上的舞者 on 10/19/16.
    //  Copyright © 2016 键盘上的舞者. All rights reserved.
    //
    
    #import "RDFontAttribute.h"
    
    @implementation RDFontAttribute
    
    - (NSString *)attributeName {
        return NSFontAttributeName;
    }
    
    - (id)attributeValue {
        if (self.font) {
            return self.font;
        } else {
            return [UIFont systemFontOfSize:15];
        }
    }
    
    @end

    //
    //  RDForegroundAttribute.h
    //  POP动画
    //
    //  Created by 键盘上的舞者 on 10/19/16.
    //  Copyright © 2016 键盘上的舞者. All rights reserved.
    //
    
    #import "RDStringAttribute.h"
    
    @interface RDForegroundAttribute : RDStringAttribute
    
    @property (nonatomic, strong) UIColor *color;
    
    @end

    //
    //  RDForegroundAttribute.m
    //  POP动画
    //
    //  Created by 键盘上的舞者 on 10/19/16.
    //  Copyright © 2016 键盘上的舞者. All rights reserved.
    //
    
    #import "RDForegroundAttribute.h"
    
    @implementation RDForegroundAttribute
    
    - (NSString *)attributeName {
        return NSForegroundColorAttributeName;
    }
    
    - (id)attributeValue {
        if (self.color) {
            return self.color;
        } else {
            return [UIColor blackColor];
        }
    }
    
    @end

    如果你需要有更多的属性,你就再自行另外去定义另外的。

    然后给NSMutableAttributeString这个系统类增加一个Category

    //
    //  NSMutableAttributedString+StringAttribute.h
    //  POP动画
    //
    //  Created by 键盘上的舞者 on 10/19/16.
    //  Copyright © 2016 键盘上的舞者. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "RDStringAttributeProtocol.h"
    
    @interface NSMutableAttributedString (StringAttribute)
    /**
     *  添加富文本属性
     *
     *  @param attribute 属性
     */
    - (void)addStringAttribute:(id <RDStringAttributeProtocol>)attribute;
    /**
     *  移除富文本属性
     *
     *  @param attribute 属性
     */
    - (void)removeStringAttribute:(id <RDStringAttributeProtocol>)attribute;
    
    @end

    //
    //  NSMutableAttributedString+StringAttribute.m
    //  POP动画
    //
    //  Created by 键盘上的舞者 on 10/19/16.
    //  Copyright © 2016 键盘上的舞者. All rights reserved.
    //
    
    #import "NSMutableAttributedString+StringAttribute.h"
    
    @implementation NSMutableAttributedString (StringAttribute)
    
    - (void)addStringAttribute:(id<RDStringAttributeProtocol>)attribute {
        [self addAttribute:[attribute attributeName]
                     value:[attribute attributeValue]
                     range:[attribute effectiveStringRange]];
    }
    
    
    - (void)removeStringAttribute:(id<RDStringAttributeProtocol>)attribute {
        [self removeAttribute:[attribute attributeName]
                        range:[attribute effectiveStringRange]];
    }
    
    @end

    那么你以后使用富文本的时候,可以如下去使用:

    RDFontAttribute *totalFont = [RDFontAttribute new];
        totalFont.font = [UIFont systemFontOfSize:20.f];
        totalFont.effectiveRange = _manage.contentRange;
        
        RDFontAttribute *valueFont = [RDFontAttribute new];
        valueFont.font = [UIFont systemFontOfSize:40.f];
        valueFont.effectiveRange = [[[_manage rangesFromPartName:@"value" withOptions:0] firstObject] rangeValue];
        
        RDForegroundAttribute *totalColor = [RDForegroundAttribute new];
        totalColor.color = [UIColor blackColor];
        totalColor.effectiveRange = _manage.contentRange;
        
        RDForegroundAttribute *valueColor = [RDForegroundAttribute new];
        valueColor.color = [self valueColorWithValue:currentValue / 100.f];
        valueColor.effectiveRange = [[[_manage rangesFromPartName:@"value" withOptions:0] firstObject] rangeValue];
        
        RDForegroundAttribute *suffixColor = [RDForegroundAttribute new];
        suffixColor.color = [self suffixColorWithValue:currentValue / 100.f];
        suffixColor.effectiveRange = [[[_manage rangesFromPartName:@"suffix" withOptions:0] firstObject] rangeValue];
        
        NSMutableAttributedString *richString = [[NSMutableAttributedString alloc] initWithString:totalString];
        [richString addStringAttribute:totalFont];
        [richString addStringAttribute:valueFont];
        [richString addStringAttribute:totalColor];
        [richString addStringAttribute:valueColor];
        [richString addStringAttribute:suffixColor];
        
        _countLael.attributedText = richString;

    这样,一个简易的封装就完成了~



沪ICP备19023445号-2号
友情链接