iOS富文本-NSAttributedString简单封装

直接调用系统的写起来比较麻烦,封装一下

因为要简单所以就写类方法

WJAttributeStyle 基类

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
/* *
 *  基类富文本
 
*/
@interface WJAttributeStyle : NSObject

@property (nonatomic,strong)NSString *attributeName;
@property (nonatomic,strong) id value;
@property (nonatomic,assign)NSRange range;

+ (WJAttributeStyle *)attributeName:(NSString *)attributeName value:( id)value range:(NSRange)range;

@end
#import  " WJAttributeStyle.h "

@implementation WJAttributeStyle

+ (WJAttributeStyle *)attributeName:(NSString *)attributeName value:( id)value range:(NSRange)range {
    WJAttributeStyle *style = [[self  classnew];
    style.attributeName = attributeName;
    style.value = value;
    style.range = range;
     return style;
}

@end

 衍生,继承于上面的基类封装颜色,和大小之后写起来会更简单

颜色:

#import  " WJAttributeStyle.h "
#import  " WJForeGroundColorStyle.h "

/* *
 *  颜色富文本
 
*/
@interface WJForeGroundColorStyle : WJAttributeStyle

+ (WJForeGroundColorStyle *)withColor:(UIColor *)color range:(NSRange)range;

@end
#import  " WJForeGroundColorStyle.h "

@implementation WJForeGroundColorStyle

+ (WJForeGroundColorStyle *)withColor:(UIColor *)color range:(NSRange)range {
    WJForeGroundColorStyle *style =
    (WJForeGroundColorStyle *)[WJForeGroundColorStyle attributeName:NSForegroundColorAttributeName value:color range:range];
     return style;
}

@end

 大小:

#import  " WJAttributeStyle.h "
/* *
 *  大小富文本
 
*/
@interface WJFontStyle : WJAttributeStyle

+ (WJFontStyle *)withFonte:(UIFont *)font range:(NSRange)range;

@end
#import  " WJFontStyle.h "

@implementation WJFontStyle

+ (WJFontStyle *)withFonte:(UIFont *)font range:(NSRange)range {
    WJFontStyle *style =
    (WJFontStyle *)[WJFontStyle attributeName:NSFontAttributeName value:font range:range];
     return style;
}
@end

 然后用个类目来给字符串设置属性文字

#import <Foundation/Foundation.h>
#import  " WJAttributeStyle.h "
#import  " WJForeGroundColorStyle.h "
#import  " WJFontStyle.h "
@interface NSString (WJAttributeStyle)

/* *
 *  根据styles数组创建出富文本
 *
 *  @param styles WJAttributeStyle对象构成的数组
 *
 *  @return 富文本
 
*/
- (NSAttributedString *)createAttributeStringWithStyles:(NSArray *)styles;

@end
#import  " NSString+WJAttributeStyle.h "

@implementation NSString (WJAttributeStyle)

- (NSAttributedString *)createAttributeStringWithStyles:(NSArray *)styles {
     if (self.length <=  0) {
         return nil;
    }
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:self];
     for ( int i =  0; i < styles.count; i ++) {
        WJAttributeStyle *style = styles[i];
        [attributeString addAttribute:style.attributeName
                                value:style.value
                                range:style.range];
        
    }
     return attributeString;
}

@end

使用:

    NSString * string =  @" 这是一个测试 ";
    _label.attributedText = [ string createAttributeStringWithStyles:
                             @[[WJAttributeStyle attributeName:NSForegroundColorAttributeName
                                                         value:[UIColor redColor]
                                                         range:NSMakeRange( 01)],
                               [WJForeGroundColorStyle withColor:[UIColor grayColor] range:NSMakeRange( 11)],
                               [WJFontStyle withFonte:[UIFont systemFontOfSize: 30] range:NSMakeRange( 21)]
                               ]];

扩展UIKit:https://github.com/YouXianMing/BookTextView

开源富文本:https://github.com/nicolasgoutaland/GONMarkupParser

图文混排:https://github.com/12207480/TYAttributedLabel

 

转载于:https://www.cnblogs.com/hxwj/p/4663088.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值