iOS 7系列译文:认识 TextKit




- NSTextStorage *textStorage = [NSTextStorage new];
- NSLayoutManager *layoutManager = [NSLayoutManager new];
- [textStorage addLayoutManager: layoutManager];
- NSTextContainer *textContainer = [NSTextContainer new];
- [layoutManager addTextContainer: textContainer];
- UITextView *textView = [[UITextView alloc] initWithFrame:someFrame
- textContainer:textContainer];
- NSTextStorage *sharedTextStorage = originalTextView.textStorage;
- NSLayoutManager *otherLayoutManager = [NSLayoutManager new];
- [sharedTextStorage addLayoutManager: otherLayoutManager];
- NSTextContainer *otherTextContainer = [NSTextContainer new];
- [otherLayoutManager addTextContainer: otherTextContainer];
- UITextView *otherTextView = [[UITextView alloc] initWithFrame:someFrame
- textContainer:otherTextContainer];
- NSTextContainer *thirdTextContainer = [NSTextContainer new];
- [otherLayoutManager addTextContainer: thirdTextContainer];
- UITextView *thirdTextView = [[UITextView alloc] initWithFrame:someFrame
- textContainer:thirdTextContainer];
- otherTextView.scrollEnabled = NO;
- - (NSString *)string;
- - (NSDictionary *)attributesAtIndex:(NSUInteger)location
- effectiveRange:(NSRangePointer)range;
- - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;
- - (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
- @implementation TKDHighlightingTextStorage
- {
- NSMutableAttributedString *_imp;
- }
- - (id)init
- {
- self = [super init];
- if (self) {
- _imp = [NSMutableAttributedString new];
- }
- return self;
- }
- - (NSString *)string
- {
- return _imp.string;
- }
- - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range
- {
- return [_imp attributesAtIndex:location effectiveRange:range];
- }
- - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str
- {
- [_imp replaceCharactersInRange:range withString:str];
- [self edited:NSTextStorageEditedCharacters range:range
- changeInLength:(NSInteger)str.length - (NSInteger)range.length];
- }
- - (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range
- {
- [_imp setAttributes:attrs range:range];
- [self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
- }
- _textStorage = [TKDHighlightingTextStorage new];
- [_textStorage addLayoutManager: self.textView.layoutManager];
- - (void)processEditing
- {
- [super processEditing];
- static NSRegularExpression *iExpression;
- NSString *pattern = @"i[\\p{Alphabetic}&&\\p{Uppercase}][\\p{Alphabetic}]+";
- iExpression = iExpression ?: [NSRegularExpression regularExpressionWithPattern:pattern
- options:0
- error:NULL];
- NSRange paragaphRange = [self.string paragraphRangeForRange: self.editedRange];
- [self removeAttribute:NSForegroundColorAttributeName range:paragaphRange];
- [iExpression enumerateMatchesInString:self.string
- options:0 range:paragaphRange
- usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
- {
- [self addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:result.range];
- }];
- }

- - (CGFloat) layoutManager:(NSLayoutManager *)layoutManager
- lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex
- withProposedLineFragmentRect:(CGRect)rect
- {
- return floorf(glyphIndex / 100);
- }
- static NSDataDetector *linkDetector;
- linkDetector = linkDetector ?: [[NSDataDetector alloc] initWithTypes:NSTextCheckingTypeLink error:NULL];
- NSRange paragaphRange = [self.string paragraphRangeForRange: NSMakeRange(range.location, str.length)];
- [self removeAttribute:NSLinkAttributeName range:paragaphRange];
- [linkDetector enumerateMatchesInString:self.string
- options:0
- range:paragaphRange
- usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
- {
- [self addAttribute:NSLinkAttributeName value:result.URL range:result.range];
- }];
- - (BOOL)layoutManager:(NSLayoutManager *)layoutManager shouldBreakLineByWordBeforeCharacterAtIndex:(NSUInteger)charIndex
- {
- NSRange range;
- NSURL *linkURL = [layoutManager.textStorage attribute:NSLinkAttributeName
- atIndex:charIndex
- effectiveRange:&range];
- return !(linkURL && charIndex > range.location && charIndex <= NSMaxRange(range));
- }


- - (void)updateExclusionPaths
- {
- CGRect ovalFrame = [self.textView convertRect:self.circleView.bounds
- fromView:self.circleView];
- ovalFrame.origin.x -= self.textView.textContainerInset.left;
- ovalFrame.origin.y -= self.textView.textContainerInset.top;
- UIBezierPath *ovalPath = [UIBezierPath bezierPathWithOvalInRect:ovalFrame];
- self.textView.textContainer.exclusionPaths = @[ovalPath];
- }
原文链接:Max Seelemann 翻译:伯乐在线 - 和谐老约翰
iOS 7系列译文:认识 TextKit的更多相关文章
- iOS开发系列--Swift语言
概述 Swift是苹果2014年推出的全新的编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程中不仅保留了ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在 ...
- iOS开发系列文章(持续更新……)
iOS开发系列的文章,内容循序渐进,包含C语言.ObjC.iOS开发以及日后要写的游戏开发和Swift编程几部分内容.文章会持续更新,希望大家多多关注,如果文章对你有帮助请点赞支持,多谢! 为了方便大 ...
- iOS开发系列--App扩展开发
概述 从iOS 8 开始Apple引入了扩展(Extension)用于增强系统应用服务和应用之间的交互.它的出现让自定义键盘.系统分享集成等这些依靠系统服务的开发变成了可能.WWDC 2016上众多更 ...
- iOS开发系列--Swift进阶
概述 上一篇文章<iOS开发系列--Swift语言>中对Swift的语法特点以及它和C.ObjC等其他语言的用法区别进行了介绍.当然,这只是Swift的入门基础,但是仅仅了解这些对于使用S ...
- iOS开发系列--通知与消息机制
概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知机制就可以告诉用户此时发生的事情.iOS中通知机制又叫消息机制,其包括两类:一类是本地 ...
- iOS开发系列--数据存取
概览 在iOS开发中数据存储的方式可以归纳为两类:一类是存储为文件,另一类是存储到数据库.例如前面IOS开发系列-Objective-C之Foundation框架的文章中提到归档.plist文件存储, ...
- iOS开发系列--网络开发
概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博.微信等,这些应用本身可能采用iOS开发,但是所有的数据支撑都是基于后台网络服务器的.如今,网络编程越来越普遍,孤立的应用通常是没有生命力 ...
- iOS开发系列--C语言之基础知识
概览 当前移动开发的趋势已经势不可挡,这个系列希望浅谈一下个人对IOS开发的一些见解,这个IOS系列计划从几个角度去说IOS开发: C语言 OC基础 IOS开发(iphone/ipad) Swift ...
- iOS开发系列--让你的应用“动”起来
--iOS核心动画 概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互式绘图,如何通过核心动画创建 ...
随机推荐
- 基于XML的AOP配置
创建spring的配置文件并导入约束 此处要导入aop的约束 <?xml version="1.0" encoding="UTF-8"?> < ...
- 乐字节Java8核心特性之Optional类
大家好啊,上次小乐给大家介绍了Java8最最重要的一个特性——Stream流,点击可以回顾哦. Optional<T>类(java.util.Optional)是一个容器类,代表一个值存在 ...
- DNS解析工具--nslookup和dig使用
1.nslookup使用 [root@master ~]# nslookup> server 8.8.8.8 #指定域名服务器Default server: 8.8.8.8Address: ...
- EcmaScript学习
1.eval: ts: declare function eval(x: string): any; js: /** @param {*} x @return {Object} */ eval = f ...
- jquery——整屏滚动
从这里下载了滚轮事件插件:https://github.com/jquery/jquery-mousewheel 函数节流:js中有些事件的触发频率非常高,在短时间内多次触发执行绑定函数,比如mous ...
- CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作
题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...
- B - Reverse and Compare 小小思维题
http://agc019.contest.atcoder.jp/tasks/agc019_b 一开始的做法是, 用总数减去回文子串数目,因为回文子串怎么翻转都不影响答案. 然后,如果翻转afucka ...
- Java面向对象_对象一一对应关系和this关键字
一.打个比方,一个人有一个身份证号,一个身份证号对应一个人.一个英雄对应一把武器,一把武器对应一个英雄.生活中很多对象都存在一一对应关系,那么一一对应关系在代码中是如何实现的呢?举个例子,英雄和武器一 ...
- Java面向对象_常用类库api——二分查找算法
概念:又称为折半查找,优点是比较次数少,查找速度快,平均性能好:缺点是要求待查表为有序表,且插入删除困难.因此,折半查找方法适用于不经常变动而查找频繁的有序列表. 例: public class Bi ...
- (转)在CentOS中修改中文字符集
虽然在实际工作环境下,Linux中不建议使用中文,但是如果一定要进行中文显示,尤其对于刚接触linux且英语基础不太好的人来说,那么本文具有一定的参考价值. 本文介绍在linux的shell环境下优化 ...