理解NSTextContainer
Apple的Doc对这个类的描述是这样的:
The NSTextContainer class defines a region where text is laid out. An NSLayoutManager uses NSTextContainer to determine where to break lines, lay out portions of text, and so on. NSTextContainer defines rectangular regions, but you can create subclasses that define regions of other shapes, such as circular regions, regions with holes in them, or regions that flow alongside graphics.
NSTextContainer定义了一个摆放text的区域。NSLayoutManager就是使用这个区域来决定何时break line,排放部分text或者其他行为。NSTextContainer定义了矩形区域,但可以继承这个类并定义任意形状的区域。
TextLayoutDemo定义了一个圆形区域,以此为例,学习一下。
- (NSRect)lineFragmentRectForProposedRect:(NSRect)proposedRect sweepDirection:(NSLineSweepDirection)sweepDirection movementDirection:(NSLineMovementDirection)movementDirection remainingRect:(NSRectPointer)remainingRect
这个方法是这个类的核心方法。proposedRect是一个提议的矩形区域,但这个区域的宽度、位置可能很不靠谱,比如width可能非常大,远超过bounding rectangle。
The receiver should examine proposedRect to see that it intersects its bounding rectangle and should return a modified rectangle based on sweepDirection and movementDirection, whose possible values are listed in the class description. If sweepDirection is NSLineSweepRight, for example, the receiver uses this information to trim the right end of proposedRect as needed rather than the left end.
If proposedRect doesn’t completely overlap the region along the axis of movementDirection and movementDirection isn’t NSLineDoesntMove, this method can either shift the rectangle in that direction as much as needed so that it does completely overlap, or return NSZeroRect to indicate that the proposed rectangle simply doesn’t fit.
这里提到了sweepDirection,这个参数的可选值有:NSLineSweepLeft , NSLineSweepRight ,NSLineSweepDown , NSLineSweepUp 。这个参数(line sweep) 描述行内text的方向,大部分text都是从左向右书写的,但是繁体中文的书有不少都是从上往下书写的,而阿拉伯文是从右往左书写的。我们计算可用空间与proposedRect的intersect时,需要考虑文字的书写方向。
另外一个参数是movementDirection,这个参数的可选值有: NSLineDoesntMove , NSLineMovesLeft , NSLineMovesRight,NSLineMovesDown , NSLineMovesUp。也就是写完一行后,下一行往哪里写?上下左右都可以,还有就是不动DontMove。
When the typesetter generates line fragments, the text container is particularly concerned with the direction in which text layout proceeds. There are two aspects to layout direction: line sweep and line movement. Line sweep is the direction in which the system lays out glyphs within lines. Line movement is the direction in which the system lays out lines upon the page. The typesetter object determines these parameters and passes them as constant values to the text container. Both line sweep and line movement can proceed from left to right, right to left, top to bottom, and bottom to top. In addition, the typesetter can specify no line movement.
Note: The built-in typesetters currently support only top-to-bottom line movement and left-to-right sweep. These typesetters do handle bidirectional text (Hebrew and Arabic) by laying it out in the proper display order within the line fragments, but they do not use the line sweep mechanism.
Apple这是够扯淡,本来sweep就是用来支持Arabic的一种技术,可是偏到这个时候又不管用了,用什么别的技术实现,这是说明API设计的不够好吗?
Reference
1. https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/TextStorageLayer/Concepts/LayoutGeometry.html#//apple_ref/doc/uid/20001803-CJBJHGAG
2. https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSTextContainer_Class/Reference/Reference.html#//apple_ref/doc/uid/20000368-1951
理解NSTextContainer的更多相关文章
- TextKit学习(三)NSTextStorage,NSLayoutManager,NSTextContainer和UITextView
先上一张图: 这是使用UITextView时用到的iOS7新增加的类:NSTextContainer.NSLayoutManager.NSTextStorage及其相互关系: 这三个新出的类还没有在官 ...
- 理解CSS视觉格式化
前面的话 CSS视觉格式化这个词可能比较陌生,但说起盒模型可能就恍然大悟了.实际上,盒模型只是CSS视觉格式化的一部分.视觉格式化分为块级和行内两种处理方式.理解视觉格式化,可以确定得到的效果是应 ...
- 彻底理解AC多模式匹配算法
(本文尤其适合遍览网上的讲解而仍百思不得姐的同学) 一.原理 AC自动机首先将模式组记录为Trie字典树的形式,以节点表示不同状态,边上标以字母表中的字符,表示状态的转移.根节点状态记为0状态,表示起 ...
- 理解加密算法(三)——创建CA机构,签发证书并开始TLS通信
接理解加密算法(一)--加密算法分类.理解加密算法(二)--TLS/SSL 1 不安全的TCP通信 普通的TCP通信数据是明文传输的,所以存在数据泄露和被篡改的风险,我们可以写一段测试代码试验一下. ...
- node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理
一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...
- 如何一步一步用DDD设计一个电商网站(一)—— 先理解核心概念
一.前言 DDD(领域驱动设计)的一些介绍网上资料很多,这里就不继续描述了.自己使用领域驱动设计摸滚打爬也有2年多的时间,出于对知识的总结和分享,也是对自我理解的一个公开检验,介于博客园这个平 ...
- 学习AOP之透过Spring的Ioc理解Advisor
花了几天时间来学习Spring,突然明白一个问题,就是看书不能让人理解Spring,一方面要结合使用场景,另一方面要阅读源代码,这种方式理解起来事半功倍.那看书有什么用呢?主要还是扩展视野,毕竟书是别 ...
- ThreadLocal简单理解
在java开源项目的代码中看到一个类里ThreadLocal的属性: private static ThreadLocal<Boolean> clientMode = new Thread ...
- JS核心系列:理解 new 的运行机制
和其他高级语言一样 javascript 中也有 new 运算符,我们知道 new 运算符是用来实例化一个类,从而在内存中分配一个实例对象. 但在 javascript 中,万物皆对象,为什么还要通过 ...
随机推荐
- Tiny PXE Server简介
Tiny PXE Server简介Tiny PXE Server是一款小巧而功能强大的网启软件.支持DHCP TFTP HTTP BINL DNS等多个协议,支持grub4dos,pxelinux,i ...
- EL表达式 (详解)(转)
EL表达式 1.EL简介 1)语法结构 ${expression} 2)[]与.运算符 EL 提供.和[]两种运算符来存取数据. 当要存取的属性名称中包含一 ...
- java 集合(ArrayList)
ArrayList: ------------|Collection 单列集合的跟接口 ----------------------|List 有序,可重复. ------------------- ...
- 小div在大div中垂直居中,以及div在页面垂直居中
<html> <head> <title>淘宝 2faner</title> <style type="text/css"&g ...
- OneProxy分库分表演示--楼方鑫
OneProxy分库分表演示 (杭州平民软件有限公司) OneProxy是为MySQL精心设计的数据访问层,可以为任何开发语言提供对MySQL数据库的智能数据路由功能,比如单点切换.读写分离.分库分表 ...
- c++相关的类型推导
c++11和boost库增加许多关于类型推导(编译期)的关键字和类型, 用好这些机制, 对于编写项目的一些组件帮助颇大.正所谓工欲善其事,必先利其器. 1.初始化某种类型的变量 auto var = ...
- 纯css3代码写下拉菜单效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Servlet后续的尾(yi)巴--------Filter过滤器
-------载录自 http://www.blogjava.net/yangaiyou/archive/2007/08/29/140854.html 感谢博主心如止水 一心向佛 描写出这么的具体 ...
- JavaScrip的DOM操作(13号讲)
1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型,文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西 2.Windows对象操作 一.属性和方法 二.Window.open(& ...
- uva 12186
12186 - Another Crisis Time limit: 3.000 seconds A couple of years ago, a new world wide crisis star ...