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本地搭建,如果一个页面需要在服务器端获取数据的话,我们也要在本地搭建好固定的布局,解析服务器传回 ...
随机推荐
- Cygwin ssh服务配置 (SecureCRT连接Cygwin配置)
1.运行ssh-host-config 这里需要注意的是标红部分,输入的用户名或密码要符合计算机的用户名或密码策略(尤其是公司有权限限制的电脑). $ ssh-host-config *** Quer ...
- 调用微信退款接口时出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法
我总结了一下出现证书无法加载的原因有以下三个 1.证书密码不正确,微信证书密码就是商户号 解决办法:请检查证书密码是不是和商户号一致 2.IIS设置错误,未加载用户配置文件 解决办法:找到网站使用的应 ...
- EF 随机排序
/// <summary> /// 数据上下文扩展 /// </summary> public partial class dbDataContext : IUnitOfWor ...
- Easy-UI 动态添加DataGrid的Toolbar按钮
在前人的基础上进行的修改,不知道他是从哪里引用来的,所以没有粘贴引用地址. 原代码不支持1.3.6. 修改功能: 1.如果之前没有添加过工具,用这个方法不能添加(已修复): 2.估计是不支持1.3.6 ...
- git用法
chapter: 8 add 添加文件内容至索引 用法:git add [选项] [--] ... -n, --dry-run 演习 -v, --verbose 冗长输出 -i, --interact ...
- OpenGl学习笔记3之模型变换、视图变换、投影变换、视口变换介绍
模型变换.视图变换.投影变换.视口变换介绍 opengl中存在四种变换,分别是模型变换,视图变换,投影变换,视口变换.这四种变换是图形渲染的基本操作,实质上这四种变换都是由矩阵乘法表示(这些操作都是由 ...
- P3245: 最快路线
这道题其实还是不难的,只是自己搞混了=-=//晕,做了好久啊,其实就是个spfa,关键是存储路径搞昏了.输出格式要求太严了,航模不能有空格啊,所以因为格式WA了三次,哭啊/(ㄒoㄒ)/~~.贴上代码吧 ...
- [shell基础]——if/for/while/until/case 语句
for语句 do echo $loop done ` do echo $loop done for loop in `ls /tmp` do echo $loop done while语句 while ...
- 设置搜狗输入法在任何时候按左右两侧的shift激活
如上图,搜狗输入法for linux最近与广大用户见面了,现在的版本是1.0.0.0014,本人系统是ubuntu 14.04非麒麟版本 使用过程中有个习惯就是在任何窗口内只要按任意一侧的shift就 ...
- 理解CSS居中
我想很多在前端学习或者开发过程中,肯定会遇到如何让你的元素居中的问题,网上google肯定会有很多的解决方法.今天我就个人的项目与学习经验谈谈个人理解css如何让元素居中. 要理解css的居中,首先必 ...