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本地搭建,如果一个页面需要在服务器端获取数据的话,我们也要在本地搭建好固定的布局,解析服务器传回 ...
随机推荐
- Nunit单元测试的使用
先建立一个需要测试的项目 安装nunit 通过nuget安装Install-Package Nunit 类前加[TestFixture] 要测试的方法前加[Test] using System; u ...
- Maven:mirror和repository 区别
1 Repository(仓库) 1.1 Maven仓库主要有2种: remote repository:相当于公共的仓库,大家都能访问到,一般可以用URL的形式访问 local repository ...
- Spring源码下载
Spring已经将源码从SVN迁移到了Github,而且也改为基于Gradle的构建来构建项目,它取代了之前的ANT+Ivy系统,所以要构建Spring源码要先安装Github和Gradle. 首先假 ...
- Stream,Reader/Writer,Buffered的区别(1)
Stream: 是字节流形式,exe文件,图片,视频等.支持8位的字符,用于 ASCII 字符和二进制数据. Reader/Writer: 是字符流,文本文件,XML,txt等,用于16位字符,也就是 ...
- golang的内存模型与new()与make()
要彻底理解new()与make()的区别, 最好从内存模型入手. golang属于c family, 而c程序在unix的内在模型: |低地址|text|data|bss|heap-->|unu ...
- random_names随机名字生成
// 先从txt文件中获取姓和名数组 - (void)getNames{ NSString *resourcePath1 = [[NSBundle mainBundle] pathForResourc ...
- IOS之表视图添加搜索栏
下面是我们要实现的效果.本效果是在上一篇自定义表视图的基础上进行更改的. 1.将Search bar and search display拖动到ViewController中.不要添加Sear ...
- sharepoint 2010 误删除AD组用户不能访问
不小心误操作把ad中的组删除了,在sharepoint中是通过组给的权限,在ad中新建了一个同样名的组给了权限组下面的用户还是不能访问. 解决方法: 在sharepoint中把这组从网站集中删除,重新 ...
- 【BZOJ 1033】 [ZJOI2008]杀蚂蚁antbuster
Description 最近,佳佳迷上了一款好玩的小游戏:antbuster.游戏规则非常简单:在一张地图上,左上角是蚂蚁窝,右下角是蛋糕,蚂蚁会源源不断地从窝里爬出来,试图把蛋糕搬回蚂蚁窝.而你的任 ...
- cocos游戏的真正入口,用C++实现的demo版本
1.cocos游戏的出发点 在main函数中有一句: return CCApplication::sharedApplication()->run(); 2.经过层层深入发现,真正的入口: ...