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 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简单封装的更多相关文章
- ios富文本的简单使用 AttributedString
富文本,顾名思义就是丰富的文本格式,本文demo使用NSMutableAttributedString //获取富文本 NSMutableAttributedString*attributeStrin ...
- iOS - UILabel添加图片之富文本的简单应用
//创建富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" ...
- iOS富文本组件的实现—DTCoreText源码解析 数据篇
本文转载 http://blog.cnbang.net/tech/2630/ DTCoreText是个开源的iOS富文本组件,它可以解析HTML与CSS最终用CoreText绘制出来,通常用于在一些需 ...
- UILabel添加图片之富文本的简单应用
若想对UILabel添加图片,那么就需要使用NSMutableAttributedString来定义先定义一个普通的label UILabel *lab = [[UILabel alloc]initW ...
- UEditor富文本编辑器简单使用
UEditor富文本编辑器简单使用 一.下载地址:https://ueditor.baidu.com/website/ 官网中并没有 python 版本的 UEditor 富文本编辑器,本文简单介绍 ...
- iOS - 富文本AttributedString
最近项目中用到了图文混排,所以就研究了一下iOS中的富文本,打算把研究的结果分享一下,也是对自己学习的一个总结. 在iOS中或者Mac OS X中怎样才能将一个字符串绘制到屏幕上呢? ...
- iOS富文本
背景:前些天突然想做一个笔记本功能,一开始,觉得挺简单的呀,一个UITextView,网络缓存也不干了,直接本地NSUserDefault存储,然后完事了,美工,弄几张好看的图片,加几个动画,也就这样 ...
- iOS富文本(一)属性化字符串
概述 iOS一些复杂的文本布局一般都是由底层的Core Text来实现的,直到iOS7苹果发布了Text Kit框架,Text Kit能够很简单实现一些复杂的文本样式以及布局,而Text Kit富文本 ...
- OS开发小记:iOS富文本框架DTCoreText在UITableView上的使用
要在页面中显示自己的布局,比如文字的字体和颜色.图文并排的样式,我们要用iOS SDK的原生UI在app本地搭建,如果一个页面需要在服务器端获取数据的话,我们也要在本地搭建好固定的布局,解析服务器传回 ...
随机推荐
- 关于HTML中,绝对定位,相对定位的理解...(学习HTML过程中的小记录)
关于HTML中,绝对定位,相对定位的理解...(学习HTML过程中的小记录) 作者:王可利(Star·星星) HTML中 相对定位:position:relative; 绝对定位:position ...
- Java中HashMap,LinkedHashMap,TreeMap的区别[转]
原文:http://blog.csdn.net/xiyuan1999/article/details/6198394 java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类 ...
- 在C#使用文件监控对象FileSystemWatcher的几种方案
最近在项目中有这么个需求,就是得去实时获取某个在无规律改变的文本文件中的内容.首先想到的是用程序定期去访问这个文件,因为对实时性要求很高,间隔不能超过1S,而且每次获取到文本内容都要去分发给web服务 ...
- 2.Modelsim打开时出现的Error
Modelsim之error “unable to check out a viewer license necessary for use of the modelsim graph.Vsim is ...
- CentOS安装Git实现多人同步开发
描 述 要开发一个"cms系统",有2个人分别是:晓飞, 盈月.要求使用Git来进行版本控制. 项目信息 版本控制:Git 项目名称:cms 开发人员:xiaofei,yingyu ...
- linux设置环境变量的方法
0.查看环境变量 export 1.直接执行命令,不过只有此次会话有效 export PATH=$PATH:/dir/I/want 2.修改profile文件 在里面加入: export PATH=& ...
- [转载]EF Code First 学习笔记:约定配置
要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...
- Spark系列—01 Spark集群的安装
一.概述 关于Spark是什么.为什么学习Spark等等,在这就不说了,直接看这个:http://spark.apache.org, 我就直接说一下Spark的一些优势: 1.快 与Hadoop的Ma ...
- [转载]解决zabbix在configure时候遇到的问题(Ubuntu)
http://hi.baidu.com/ryq1xiuluo/item/6d37e658f1b90b13db16351d ./configure --enable-server --enable-ag ...
- android讯飞语音开发常遇到的问题
场景:android项目中共使用了3个语音组件:在线语音听写.离线语音合成.离线语音识别 11208:遇到这个错误,授权应用失败,先检查装机量(3台测试权限),以及appid的申请时间(35天期限), ...