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中如何使用图层精简非交互式绘图,如何通过核心动画创建 ...
随机推荐
- Boost Python学习笔记(四)
你将学到什么 在Python中调用C++代码时的传参问题 基础类型 Python的字符串是常量,所以C++函数参数中的std::string &必须为const 修改源文件(main.cpp) ...
- restfull知识点
网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......).因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致API ...
- 51nod1024(math+set)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1024 题意:中文题诶- 思路:要是能求出a^b的值来就好了. ...
- bzoj 3722: PA2014 Final Budowa
3722: PA2014 Final Budowa Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 303 Solved: 108[Submit][St ...
- CDN working principle diagram
转自 https://cloud.tencent.com/developer/article/1358553
- Stream流、方法引用
Stream流.方法引用 Stream流.方法引用 Stream流.方法引用 Stream流.方法引用 Stream流.方法引用 ... ...
- 3Ds Max FTL:Virtual device creation failed.
1.在安装完成并激活3DsMax2017中文版后,启动提示:渲染错误消息:FTL: Virtual device creation failed.(中文译:虚拟设备的创建失败). 2.关闭渲染错误消息 ...
- js校验金额输入
//验证非负实数,保留2位小数,不采取四舍五入,不能去掉无效零,obj是this //示例:<input type="text" onkeyup="IWS_Chec ...
- UVa 10652(旋转、凸包、多边形面积)
要点 凸包显然 长方形旋转较好的处理方式就是用中点的Vector加上旋转的Vector,然后每个点都扔到凸包里 多边形面积板子求凸包面积即可 #include <cstdio> #incl ...
- ThreadPoolExecutor线程池的keepAliveTime
keepAliveTime含义 看了很多文章觉得都不能把keepAliveTime的意思说的很明白,希望通过自己的理解把keepAliveTime说的明确一些 先引用一句我觉得相对说的比较明白的含义: ...