iOS富文本(三)深入使用Text Kit
在上一篇中介绍了Text Kit的三种基本组件的关系并且简单的实现了怎么使用这三种基本组件,本片将深入的去使用这三种基本组件。
NSTextStorage
NSTextStorage是NSMutableAttributedString的子类,根据苹果官方文档描述是semiconcrete子类,因为NSTextStorage没有实现NSMutableAttributedString中的方法,所以说NSTextStorage应该是NSMutableAttributedString的类簇。
所要我们深入使用NSTextStorage不仅要继承NSTextStorage类还要实现NSMutableAttributedString的下面方法
- (NSString *)string
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str
- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range
因为这些方法实际上NSTextStorage并没有实现然而我们断然不知道NSMutableAttributedString是如何实现这些方法,所以我们继承NSTextStorage并实现这些方法最简单的莫过于在NSTextStorage类中实例化一个NSMutableAttributedString对象然后调用NSMutableAttributedString对象的这些方法来实现NSTextStorage类中的这些方法
@interface LSYTextStorage : NSTextStorage
@property (nonatomic,strong) NSMutableAttributedString *attributedString;
@end
继承NSTextStorage后都会实现下面的代码,如果要做一些特殊的处理知道在下面的代码里添加就可以了
#import "LSYTextStorage.h"
@interface LSYTextStorage ()
@property (nonatomic,strong) NSMutableAttributedString *attributedString;
@end
@implementation LSYTextStorage
- (instancetype)init
{
self = [super init];
if (self) {
_attributedString = [[NSMutableAttributedString alloc] init];
}
return self;
}
-(NSString *)string{
return [_attributedString string];
}
- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(nullable NSRangePointer)range
{
return [_attributedString attributesAtIndex:location effectiveRange:range];
}
-(void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str
{
[self beginEditing];
[_attributedString replaceCharactersInRange:range withString:str];
[self edited:NSTextStorageEditedAttributes|NSTextStorageEditedCharacters range:range changeInLength:str.length-range.length];
[self endEditing];
}
-(void)setAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range
{
[self beginEditing];
[_attributedString setAttributes:attrs range:range];
[self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
[self endEditing];
}
@end
上面实现的方法里代码都加上了beginEditing,edited:range:changeInLength:,endEditing的方法,这样做主要是通知它的 Layout Manager 发生了变化来计时调整布局
根据上面提供的模版添加特殊处理的代码
#import "LSYTextStorage.h"
@interface LSYTextStorage ()
@property (nonatomic,strong) NSMutableAttributedString *attributedString;
@end
@implementation LSYTextStorage
- (instancetype)init
{
self = [super init];
if (self) {
_attributedString = [[NSMutableAttributedString alloc] init];
}
return self;
}
-(NSString *)string{
return [_attributedString string];
}
- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(nullable NSRangePointer)range
{
return [_attributedString attributesAtIndex:location effectiveRange:range];
}
-(void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str
{
[self beginEditing];
[_attributedString replaceCharactersInRange:range withString:str];
[self edited:NSTextStorageEditedAttributes|NSTextStorageEditedCharacters range:range changeInLength:str.length-range.length];
[self endEditing];
}
-(void)setAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range
{
[self beginEditing];
[_attributedString setAttributes:attrs range:range];
[self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
[self endEditing];
}
-(void)processEditing
{
NSRange lineRange = NSUnionRange(self.editedRange, [self.string lineRangeForRange:self.editedRange]); //正在编辑的整个段落范围
[self.attributedString.string enumerateSubstringsInRange:lineRange options:NSStringEnumerationByWords usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
if ([substring isEqualToString:@"GGGHub"]) {
[self setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:substringRange]; //当出现GGGHub单词时字体变红
}
else{
[self setAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} range:substringRange]; //默认字体是黑色
}
}];
[super processEditing];
}
@end
每次编辑都会调用-(void)processEditing的方法,然后遍历整段修改的文字当出现GGGHub的单词时显示红色字体。
#import "ViewController.h"
#import "LSYTextStorage.h"
@interface ViewController ()
{
LSYTextStorage *textStroage; //需要声明为全局变量,否则出了作用域后就释放掉了
}
@property (weak, nonatomic) IBOutlet UITextView *textView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *str = _textView.text;
textStroage = [[LSYTextStorage alloc] init];
[textStroage replaceCharactersInRange:NSMakeRange(0, 0) withString:str];
[textStroage addLayoutManager:self.textView.layoutManager];
//替换textView的textStroage属性
}
只要文本中出现指定的关键字字体就会变红,输入指定的关键字字体也会变红。
实现效果
代码在github NSTextStorage Tag下载
NSLayoutManager
布局管理器主要用来绘制字体的。NSTextStorage虽然能够改变字体的样式但是更改不了字体绘制的方式。我们可以继承NSLayoutManager来更改字体绘制。对于某些特定的字段可能不需要显示比如加密文本,或者用图片替换这些字段,或者給这些字段添加一些背景,这时只要重写NSLayoutManager中的某些方法可以很简单的实现。
更改字体绘制与字体背景颜色只需要重写下面的两个方法
- (void)drawBackgroundForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin;
//绘制字形背景
- (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin;
//绘制字形
下面例子会实现这样的功能,只要文本中有纯数字或者输入纯数字那么这段数字不显示出来,用黑色的遮罩挡住。
首先在LSYTextStorage.m文件中更改processEditing函数
-(void)processEditing
{
NSRange lineRange = NSUnionRange(self.editedRange, [self.string lineRangeForRange:self.editedRange]);
NSString *regexNumber = @"^-?[0-9]\\d*$";
NSPredicate *predicateNumber = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regexNumber];
//正则表达式,判断是否为纯数字
[self.attributedString.string enumerateSubstringsInRange:lineRange options:NSStringEnumerationByWords usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
NSLog(@"%@",substring);
if ([substring isEqualToString:@"GGGHub"]) {
[self setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:substringRange];
}
/**
* 如果是纯数字給这段字符串添加LSYSecretAttribute属性为了绘制字形时查找
*/
else if ([predicateNumber evaluateWithObject:substring]){
[self setAttributes:@{@"LSYSecretAttribute":@"secretAttribute"} range:substringRange];
}
else{
[self setAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} range:substringRange];
}
}];
[super processEditing];
}
上面的代码主要为纯数字的字符串添加一个LSYSecretAttribute的属性,当NSLayoutManager开始绘制字形时可以方便找到这段字符串然后在这段字符串的范围绘制黑色遮罩
再重写NSLayoutManager的方法前
下面重写NSLayoutManager的drawGlyphsForGlyphRange方法
-(void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin
{
NSRange range = [self characterRangeForGlyphRange:glyphsToShow
actualGlyphRange:NULL];
[self.textStorage enumerateAttribute:@"LSYSecretAttribute" inRange:range options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
//找到在LSYTextStorage中自定的LSYSecretAttribute属性
if ([value isEqualToString:@"secretAttribute"]) {
NSRange glyphRange = [self glyphRangeForCharacterRange:range
actualCharacterRange:NULL];
NSTextContainer *
container = [self textContainerForGlyphAtIndex:glyphRange.location
effectiveRange:NULL];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context); //保存当前的绘图配置信息
CGContextTranslateCTM(context, origin.x, origin.y); //转换初始坐标系到绘制字形的位置
[[UIColor blackColor] setFill];
CGRect rect = [self boundingRectForGlyphRange:glyphRange inTextContainer:container];
[self drawSecret:rect]; //开始绘制
CGContextRestoreGState(context); //恢复绘图配置信息
}
else
{
[super drawGlyphsForGlyphRange:range atPoint:origin];
}
}];
}
实现效果
这种遮罩是动态的,只要输入是纯数字那么NSLayoutManager的对象就不会对其进行绘制,而用黑色的遮罩挡住。
代码在github NSLayoutManager Tag下载
iOS富文本(三)深入使用Text Kit的更多相关文章
- iOS富文本组件的实现—DTCoreText源码解析 数据篇
本文转载 http://blog.cnbang.net/tech/2630/ DTCoreText是个开源的iOS富文本组件,它可以解析HTML与CSS最终用CoreText绘制出来,通常用于在一些需 ...
- iOS富文本(二)初识Text Kit
概述 Text Kit 是建立在Core Text上的文本布局系统,虽然没有Core Text那么强大的文本处理功能,但是对于大多数常见的文本布局用Text Kit能够很简单的实现,而不是用Core ...
- iOS富文本
背景:前些天突然想做一个笔记本功能,一开始,觉得挺简单的呀,一个UITextView,网络缓存也不干了,直接本地NSUserDefault存储,然后完事了,美工,弄几张好看的图片,加几个动画,也就这样 ...
- iOS - 富文本
iOS--NSAttributedString超全属性详解及应用(富文本.图文混排) ios项目中经常需要显示一些带有特殊样式的文本,比如说带有下划线.删除线.斜体.空心字体.背景色.阴影以及图文 ...
- iOS富文本(一)属性化字符串
概述 iOS一些复杂的文本布局一般都是由底层的Core Text来实现的,直到iOS7苹果发布了Text Kit框架,Text Kit能够很简单实现一些复杂的文本样式以及布局,而Text Kit富文本 ...
- OS开发小记:iOS富文本框架DTCoreText在UITableView上的使用
要在页面中显示自己的布局,比如文字的字体和颜色.图文并排的样式,我们要用iOS SDK的原生UI在app本地搭建,如果一个页面需要在服务器端获取数据的话,我们也要在本地搭建好固定的布局,解析服务器传回 ...
- iOS 富文本类库RTLabel
本文转载至 http://blog.csdn.net/duxinfeng2010/article/details/9004749 本节关于RTLable基本介绍,原文来自 https://git ...
- iOS - 富文本AttributedString
最近项目中用到了图文混排,所以就研究了一下iOS中的富文本,打算把研究的结果分享一下,也是对自己学习的一个总结. 在iOS中或者Mac OS X中怎样才能将一个字符串绘制到屏幕上呢? ...
- iOS - 富文本直接设置文字的字体大小和颜色
富文本效果图: 富文本实现代码: UILabel *orderSureLabel = [Common lableFrame:CGRectZero title:] textColor:[UIColor ...
随机推荐
- Asp.Net中用JS中操作cookie的方法
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cookies.aspx.cs& ...
- JS数据类型的理解(猜测)
Js 数据类型 对于这个主题,首先来看几个问题,如果你对这几个问题很清楚的话,那就请直接跳过吧,不用接着往下看了,如果不清楚,建议你还是看看. 1)如果判断函数?function 和object的联系 ...
- CKEditor (Toolbar Definition)工具栏自定义配置
JS是大小写敏感的, 在设置配置文件的时候需要注意 以CKEditor 4为基础我们可以通过两种方式配置CKEditor的工具栏,一种是是通过config.js配置文件设置, 另一种是IN-PAGE方 ...
- Unity使用外部版本控制SVN
原地址:http://www.cnblogs.com/realtimepixels/p/3652146.html Using External Version Control Systems with ...
- Xmarks丢失书签
想体验下Xmarks,不同浏览器同步书签,听说很好用,就安装Chrome插件,没想到竟然把我的所有书签都丢了. 不过在网上找到了回复的办法,也很简单: 原始地址:http://irising.me/2 ...
- 使用git了解代码编写过程
在看教程时,有的老师会将代码放到github,如果不想跟着视频一步一步来,那就直接clone整个代码,但整个看着又有点蒙,那就使用版本切换的功能了. 首先 git clone 下载下来 git log ...
- JAVA类型信息——反射机制
JAVA类型信息——反射机制 一.反射机制概述 1.反射机制:就是java语言在运行时拥有的一项自我观察的能力,java通过这种能力彻底了解程序自身的情况,并为下一步的动作做准备. 2.反射机制的功能 ...
- Tencent 的电话面试
Tencent的实习生招聘投了简历.然后,万万没想到昨晚腾讯IEG直接给我电话了.当时就惊呆了,我都没有找人内推,就直接电话面试了. 就为昨晚的电话面试写写感想吧!问的挺多的,基本上简历上写了的都问到 ...
- SSH 端口转发
第一部分 概述 当你在咖啡馆享受免费 WiFi 的时候,有没有想到可能有人正在窃取你的密码及隐私信息?当你发现实验室的防火墙阻止了你的网络应用端口,是不是有苦难言?来看看 SSH 的端口转发功能能给我 ...
- c# 委托 和 事件
当初学C#的时候,没有完全吃透的,只能现在继续了... 欠老账.... http://www.cnblogs.com/chengxingliang/archive/2013/05/21/305191 ...