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

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

WJAttributeStyle 基类

  1. #import <Foundation/Foundation.h>
  2. #import <UIKit/UIKit.h>
  3. /**
  4.  *  基类富文本
  5.  */
  6. @interface WJAttributeStyle : NSObject
  7.  
  8. @property (nonatomic,strong)NSString *attributeName;
  9. @property (nonatomic,strong)id value;
  10. @property (nonatomic,assign)NSRange range;
  11.  
  12. + (WJAttributeStyle *)attributeName:(NSString *)attributeName value:(id)value range:(NSRange)range;
  13.  
  14. @end
  1. #import "WJAttributeStyle.h"
  2.  
  3. @implementation WJAttributeStyle
  4.  
  5. + (WJAttributeStyle *)attributeName:(NSString *)attributeName value:(id)value range:(NSRange)range {
  6.     WJAttributeStyle *style = [[self class] new];
  7.     style.attributeName = attributeName;
  8.     style.value = value;
  9.     style.range = range;
  10.     return style;
  11. }
  12.  
  13. @end

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

颜色:

  1. #import "WJAttributeStyle.h"
  2. #import "WJForeGroundColorStyle.h"
  3.  
  4. /**
  5.  *  颜色富文本
  6.  */
  7. @interface WJForeGroundColorStyle : WJAttributeStyle
  8.  
  9. + (WJForeGroundColorStyle *)withColor:(UIColor *)color range:(NSRange)range;
  10.  
  11. @end
  1. #import "WJForeGroundColorStyle.h"
  2.  
  3. @implementation WJForeGroundColorStyle
  4.  
  5. + (WJForeGroundColorStyle *)withColor:(UIColor *)color range:(NSRange)range {
  6.     WJForeGroundColorStyle *style =
  7.     (WJForeGroundColorStyle *)[WJForeGroundColorStyle attributeName:NSForegroundColorAttributeName value:color range:range];
  8.     return style;
  9. }
  10.  
  11. @end

大小:

  1. #import "WJAttributeStyle.h"
  2. /**
  3.  *  大小富文本
  4.  */
  5. @interface WJFontStyle : WJAttributeStyle
  6.  
  7. + (WJFontStyle *)withFonte:(UIFont *)font range:(NSRange)range;
  8.  
  9. @end
  1. #import "WJFontStyle.h"
  2.  
  3. @implementation WJFontStyle
  4.  
  5. + (WJFontStyle *)withFonte:(UIFont *)font range:(NSRange)range {
  6.     WJFontStyle *style =
  7.     (WJFontStyle *)[WJFontStyle attributeName:NSFontAttributeName value:font range:range];
  8.     return style;
  9. }
  10. @end

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

  1. #import <Foundation/Foundation.h>
  2. #import "WJAttributeStyle.h"
  3. #import "WJForeGroundColorStyle.h"
  4. #import "WJFontStyle.h"
  5. @interface NSString (WJAttributeStyle)
  6.  
  7. /**
  8.  *  根据styles数组创建出富文本
  9.  *
  10.  *  @param styles WJAttributeStyle对象构成的数组
  11.  *
  12.  *  @return 富文本
  13.  */
  14. - (NSAttributedString *)createAttributeStringWithStyles:(NSArray *)styles;
  15.  
  16. @end
  1. #import "NSString+WJAttributeStyle.h"
  2.  
  3. @implementation NSString (WJAttributeStyle)
  4.  
  5. - (NSAttributedString *)createAttributeStringWithStyles:(NSArray *)styles {
  6.     if (self.length <= ) {
  7.         return nil;
  8.     }
  9.     NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:self];
  10.     for (int i = ; i < styles.count; i ++) {
  11.         WJAttributeStyle *style = styles[i];
  12.         [attributeString addAttribute:style.attributeName
  13.                                 value:style.value
  14.                                 range:style.range];
  15.         
  16.     }
  17.     return attributeString;
  18. }
  19.  
  20. @end

使用:

  1.     NSString *string = @"这是一个测试";
  2.     _label.attributedText = [string createAttributeStringWithStyles:
  3.                              @[[WJAttributeStyle attributeName:NSForegroundColorAttributeName
  4.                                                          value:[UIColor redColor]
  5.                                                          range:NSMakeRange(, )],
  6.                                [WJForeGroundColorStyle withColor:[UIColor grayColor] range:NSMakeRange(, )],
  7.                                [WJFontStyle withFonte:[UIFont systemFontOfSize:] range:NSMakeRange(, )]
  8.                                ]];

扩展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. JavaWeb之 Servlet执行过程 与 生命周期

    Servlet的概念 什么是Servlet呢? Java中有一个叫Servlet的接口,如果一个普通的类实现了这个接口,这个类就是一个Servlet.Servlet下有一个实现类叫HttpServle ...

  2. 信号驱动的IO

    (1)client1,基于SIGIO的写法: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h ...

  3. mybatis数据库基本配置包括数据源事物类型等

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...

  4. QTP获取系统时间并自定义格式

    function GetDateTime(Nowstr)          Dim Currentdatetime           Dim YY   'Year          Dim MM   ...

  5. hdu 3282 Running Median

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...

  6. python中的小技巧

    1.求1~100以内的素数 prime=filter(lambda x: not [x%i for i in range(2,x) if x%i==0], range(2,101))#列表推导,一行搞 ...

  7. [转]Linux Ubuntu上架设FTP

    Linux Ubuntu上架设FTP http://www.blogjava.net/stonestyle/articles/369104.html 操作系统:ubuntu (GNU/Linux) 为 ...

  8. Sharepoint 2010 RBS 的安装和配置

    基本上按照微软官方的配置方法配置就行了. 给下地址:微软官方     涂大神 找RBS的安装文件也是个问题,微软的链接好像失效了.我使用的是RBS下载.这里也有. 我的环境是数据库两台故障群集转移,微 ...

  9. EasyUI 兼容 IE6 方法总结

    1.combobox 如果单选,multiple必须也设置为true.这个ie7如果没设置,会保持多选状态,算是一个bug. 2.combobox 最好用js来渲染,而不是一开始就class=“eas ...

  10. MySQL 主从数据库设置

    1.复制的介绍 MySQL 支持单向.异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护文件的一个索引 以跟踪日志循环.这些日志可 ...