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

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

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 class] new];
    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 <= ) {
        return nil;
    }
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:self];
    for (int i = ; 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(, )],
                               [WJForeGroundColorStyle withColor:[UIColor grayColor] range:NSMakeRange(, )],
                               [WJFontStyle withFonte:[UIFont systemFontOfSize:] range:NSMakeRange(, )]
                               ]];

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

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

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

iOS富文本-NSAttributedString简单封装的更多相关文章

  1. ios富文本的简单使用 AttributedString

    富文本,顾名思义就是丰富的文本格式,本文demo使用NSMutableAttributedString //获取富文本 NSMutableAttributedString*attributeStrin ...

  2. iOS - UILabel添加图片之富文本的简单应用

    //创建富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" ...

  3. iOS富文本组件的实现—DTCoreText源码解析 数据篇

    本文转载 http://blog.cnbang.net/tech/2630/ DTCoreText是个开源的iOS富文本组件,它可以解析HTML与CSS最终用CoreText绘制出来,通常用于在一些需 ...

  4. UILabel添加图片之富文本的简单应用

    若想对UILabel添加图片,那么就需要使用NSMutableAttributedString来定义先定义一个普通的label UILabel *lab = [[UILabel alloc]initW ...

  5. UEditor富文本编辑器简单使用

    UEditor富文本编辑器简单使用 一.下载地址:https://ueditor.baidu.com/website/ 官网中并没有 python 版本的 UEditor 富文本编辑器,本文简单介绍 ...

  6. iOS - 富文本AttributedString

    最近项目中用到了图文混排,所以就研究了一下iOS中的富文本,打算把研究的结果分享一下,也是对自己学习的一个总结. 在iOS中或者Mac OS X中怎样才能将一个字符串绘制到屏幕上呢?         ...

  7. iOS富文本

    背景:前些天突然想做一个笔记本功能,一开始,觉得挺简单的呀,一个UITextView,网络缓存也不干了,直接本地NSUserDefault存储,然后完事了,美工,弄几张好看的图片,加几个动画,也就这样 ...

  8. iOS富文本(一)属性化字符串

    概述 iOS一些复杂的文本布局一般都是由底层的Core Text来实现的,直到iOS7苹果发布了Text Kit框架,Text Kit能够很简单实现一些复杂的文本样式以及布局,而Text Kit富文本 ...

  9. OS开发小记:iOS富文本框架DTCoreText在UITableView上的使用

    要在页面中显示自己的布局,比如文字的字体和颜色.图文并排的样式,我们要用iOS SDK的原生UI在app本地搭建,如果一个页面需要在服务器端获取数据的话,我们也要在本地搭建好固定的布局,解析服务器传回 ...

随机推荐

  1. sftp

    SFTP 为 SSH的一部分,是一种传输档案至 Blogger 伺服器的安全方式.其实在SSH软件包中,已经包含了一个叫作SFTP(Secure File Transfer Protocol)的安全文 ...

  2. EMVTag系列10《发卡行公钥证书》

    Ø  90  发卡行公钥(IPK)证书 L: NCA -C(有条件):如果支持SDA,DDA CA认证过的发卡行公钥.用于脱机数据认证 Ø  9F32    发卡行公钥指数 L: 1 or 3 -C( ...

  3. Virtual Box + CentOS Minimal + Apache搭建Web服务器

    本文并不介绍关于Virtual Box, CentOS, Apache的安装, 主要针对安装后相关的配置, 使宿主机(Host)可以访问客户机(Guest: CentOS in Virtual Box ...

  4. Android 设计模式

    简介 项目开发中发现问题.解决问题这个过程中会出现很多问题,比如重复出现.某个问题的遗留,这些问题的本质就是设计模式.今天记录设计模式的知识点. 内容 在java以及其他的面向对象设计模式中,类与类之 ...

  5. MS Chart-按照数据库的最大最小时间设置X轴label.

    核心代码: Chart1.ChartAreas[0].AxisX.Interval = (Front_Max - Front_Min).Days / 2; Chart1.ChartAreas[0].A ...

  6. 数组和字典 swift

    var array = ["A","B"] var array2:[String] = ["A","B"] var ar ...

  7. 硬件相关-ADC原理(未完成)

    一.模数转换的一般步骤: 1)采样和保持 为了把模拟信号转换成对应的数字信号,必须首先将模拟量每隔一定时间抽取一次样值,使时间上连续变化的模拟量变为一个时间上断续变化的模拟量,这个过程称为采样. 为了 ...

  8. Linux C 文件与目录3 文件读写

    文件读写 文件读写是指从文件中读出信息或将信息写入到文件中.Linux文件读取可使用read函数来实现的,文件写入可使用write函数来实现.在进行文件写入的操作时,只是在文件的缓冲区中操作,可能没有 ...

  9. Machine Learning 学习笔记 (1) —— 线性回归与逻辑回归

    本系列文章允许转载,转载请保留全文! [请先阅读][说明&总目录]http://www.cnblogs.com/tbcaaa8/p/4415055.html 1. 梯度下降法 (Gradien ...

  10. win32 sdk显示一个载入的位图的方法

    注:整理自网络文档 (1)加载位图 HANDLE LoadImage(HINSTANCE 来源实体,LPCTSTR 名称,UINT 位图类型, int 加载宽度,int 加载高度,UINT 加载方式) ...