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的更多相关文章

  1. TextKit学习(三)NSTextStorage,NSLayoutManager,NSTextContainer和UITextView

    先上一张图: 这是使用UITextView时用到的iOS7新增加的类:NSTextContainer.NSLayoutManager.NSTextStorage及其相互关系: 这三个新出的类还没有在官 ...

  2. 理解CSS视觉格式化

    前面的话   CSS视觉格式化这个词可能比较陌生,但说起盒模型可能就恍然大悟了.实际上,盒模型只是CSS视觉格式化的一部分.视觉格式化分为块级和行内两种处理方式.理解视觉格式化,可以确定得到的效果是应 ...

  3. 彻底理解AC多模式匹配算法

    (本文尤其适合遍览网上的讲解而仍百思不得姐的同学) 一.原理 AC自动机首先将模式组记录为Trie字典树的形式,以节点表示不同状态,边上标以字母表中的字符,表示状态的转移.根节点状态记为0状态,表示起 ...

  4. 理解加密算法(三)——创建CA机构,签发证书并开始TLS通信

    接理解加密算法(一)--加密算法分类.理解加密算法(二)--TLS/SSL 1 不安全的TCP通信 普通的TCP通信数据是明文传输的,所以存在数据泄露和被篡改的风险,我们可以写一段测试代码试验一下. ...

  5. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  6. 如何一步一步用DDD设计一个电商网站(一)—— 先理解核心概念

    一.前言     DDD(领域驱动设计)的一些介绍网上资料很多,这里就不继续描述了.自己使用领域驱动设计摸滚打爬也有2年多的时间,出于对知识的总结和分享,也是对自我理解的一个公开检验,介于博客园这个平 ...

  7. 学习AOP之透过Spring的Ioc理解Advisor

    花了几天时间来学习Spring,突然明白一个问题,就是看书不能让人理解Spring,一方面要结合使用场景,另一方面要阅读源代码,这种方式理解起来事半功倍.那看书有什么用呢?主要还是扩展视野,毕竟书是别 ...

  8. ThreadLocal简单理解

    在java开源项目的代码中看到一个类里ThreadLocal的属性: private static ThreadLocal<Boolean> clientMode = new Thread ...

  9. JS核心系列:理解 new 的运行机制

    和其他高级语言一样 javascript 中也有 new 运算符,我们知道 new 运算符是用来实例化一个类,从而在内存中分配一个实例对象. 但在 javascript 中,万物皆对象,为什么还要通过 ...

随机推荐

  1. phalcon: 表单

    以实例为说明: controller <?php use \Phalcon\Forms\Form; use \Phalcon\Forms\Element\Text; use \Phalcon\F ...

  2. VI打开和编辑多个文件的命令 分屏操作

    VI打开和编辑多个文件的命令 可分两种情况: 1.在同一窗口中打开多个文件: vi file1 file2 file3:n  切换到下一个文件 (n=next):N  切换到上一个文件 2.在不同窗口 ...

  3. java 多线程4(死锁)

    死锁现象: 死锁原因: 1.存在两个或两个以上的线程. 2.存在两个或两个或两个以上的共享资源. 死锁现象解决的方案: 没有方案只能尽量避免.

  4. python 练习 29

    Python Number 数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的值,将重新分配内存空间. 以下实例在变量赋值时 Number 对象将被创建: v ...

  5. Excel中的隐藏函数

    excel有一些隐藏函数,这些函数在帮助文件中找不到介绍,常用的有以下三个: 1.DATEDIF() 功能:计算两个日期的差值 语法:DATEDIF(start_date,end_date,unit) ...

  6. 20145236 冯佳 《Java程序设计》第2周学习总结

    20145236 <Java程序设计>第2周学习总结 教材学习内容总结 一.Java的基本类型. 在Java中的基本类型主要可区分为整数.字节.浮点数字符与布尔. •整数: 类型 长度 范 ...

  7. oracle第一章

    1.oracle对比sqlserver oracle sqlserver 数据文件.dbf 数据文件.mdf 控制文件.ctl   日志文件.log 日志文件.log     2.内置用户 1.sys ...

  8. SQL Server数据库(SQL Sever语言 存储过程及触发器)

    存储过程:就像函数一样的会保存在数据库中-->可编程性-->存储过程 创建存储过程: 保存在数据库表,可编程性,存储过程create proc jiafa --需要的参数@a int,@b ...

  9. java网络编程socket解析

    转载:http://www.blogjava.net/landon/archive/2013/07/02/401137.html Java网络编程精解笔记2:Socket详解 Socket用法详解 在 ...

  10. 什么是 Unix 以及它为什么这么重要?

    大多数操作系统可以被划分到两个不同的家族.除了微软的基于Windows NT的操作系统外,几乎所有其他的都可以追溯到Unix. Linux,Mac OS X,Android,iOS,Chrome OS ...