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

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

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. C 几种异常机制简单讲述

    引言 这是关于C中如何使用异常机制的讨论.顺带讲一讲C中魔法函数的setjmp内部机制.通过它实现高级的异常try...catch. 允许我先扯一段面试题. 对于计算机面试题. 算法题等.觉得还是有意 ...

  2. ED/EP系列2《文件结构》

    电子存折/电子钱包应用是为持卡人进行金融交易而设计的一种应用.对于一张金融 IC 卡来说,它可以同时支持电子存折和电子钱包两种应用,也可以只支持其中的一种.卡片上两种应用的存在情况可以由应用类型标识( ...

  3. [目录]Pentaho Kettle解决方案:使用PDI构建开源ETL解决方案

    第一部分:开始 1         ETL入门 1.1   OLTP和数据仓库对比 1.2   ETL是什么 1.2.1          ETL解决方案的演化过程 1.2.2          ET ...

  4. java 切换

    Android L之后推荐使用JDK7编译程序,这是自然发展规律,就像是4年前编译Android 1.6需要使用JDK5一样. 多版本JDK是可以共存的,只需要使用update-alternative ...

  5. go语言示例-Timer计时器的用法

    计时器用来定时执行任务,分享一段代码: package main import "time" import "fmt" func main() { //新建计时 ...

  6. 使用angular封装echarts

    Echarts是一个开源的图表组件,图表比较丰富,工作中需要用到它来搭建一个数据展示系统.但是系统原有的框架是基于angular的,而echarts是基于原生js的,如果直接使用的话就丢失了angul ...

  7. 4.FPGA芯片管脚解释

    用户I/O:不用解释了.   配置管脚: MSEL[1:0] 用于选择配置模式,比如AS.PS等. DATA0 FPGA串行数据输入,连接到配置器件的串行数据输出管脚. DCLK FPGA串行时钟输出 ...

  8. qt 焦点设置策略

    focusPolicy 一个QWidget获得焦点的方式受 focusPolicy 控制 Qt::TabFocus 通过Tab键获得焦点 Qt::ClickFocus 通过被单击获得焦点 Qt::St ...

  9. < java.util >-- Set接口

    Set接口中的方法和Collection中方法一致的.Set接口取出方式只有一种,迭代器. |--HashSet:底层数据结构是哈希表,线程是不同步的.无序,高效: HashSet集合保证元素唯一性: ...

  10. P1571: [Usaco2009 Open]滑雪课Ski

    DP. ; var t,s,n,i,j,m,l,a,c,d:longint; f,e:array[..,..maxn] of longint; q:array[..] of longint; g:ar ...