iphone - CTLine 有 "string access"是什么意思?

标签 iphone ios uilabel core-text ctlineref

我正在尝试解决 hairy problem with UILabel ,我已经弄清楚了其中的大部分内容,除了一件事:我很难理解 CTLine 具有“字符串访问权限”的含义。

我想使用的方法是 CTLineGetOffsetForStringIndexHere's a link to the documentation for the method.

这是文档中我不明白的部分(重点是我的):

The primary offset along the baseline for charIndex, or 0.0 if the line does not support string access.

当我运行这个方法时,我得到了 0.0,所以我猜这意味着该行不支持字符串访问——但这到底是什么意思?

最佳答案

语句“该行不支持字符串访问”可以推断为这行文本可能被视为可以通过每个字符的索引访问的字符序列.

这可能会引发一场关于视觉字符与非视觉字符、字形与字符的大讨论。但是为了简化讨论,假设一行文本可能具有以下状态之一:

  1. 有问题的文本行中存在超过零个字符(在同一行中转换为字形或空格的字符)
  2. 文本行中没有字符占据任何“空格”

现在为这个推论提供一些基本原理。

Apple 的文档提供了 Text Kit 的描述,UILabel 是基于它构建的:

The UIKit framework includes several classes whose purpose is to display text in an app’s user interface: UITextView, UITextField, and UILabel, as described in Displaying Text Content in iOS. Text views, created from the UITextView class, are meant to display large amounts of text. Underlying UITextView is a powerful layout engine called Text Kit. If you need to customize the layout process or you need to intervene in that behavior, you can use Text Kit. For smaller amounts of text and special needs requiring custom solutions, you can use alternative, lower-level technologies, as described in Lower Level Text-Handling Technologies.

Text Kit is a set of classes and protocols in the UIKit framework providing high-quality typographical services that enable apps to store, lay out, and display text with all the characteristics of fine typesetting, such as kerning, ligatures, line breaking, and justification. Text Kit is built on top of Core Text, so it provides the same speed and power. UITextView is fully integrated with Text Kit; it provides editing and display capabilities that enable users to input text, specify formatting attributes, and view the results. The other Text Kit classes provide text storage and layout capabilities. Figure 8-1 shows the position of Text Kit among other iOS text and graphics frameworks.

Figure 8-1 Text Kit Framework Position Figure 8-1  Text Kit Framework Position

Text Kit gives you complete control over text rendering in user interface elements. In addition to UITextView, UITextField and UILabel are built on top of Text Kit, and it seamlessly integrates with animations, UICollectionView and UITableView. Text Kit is designed with a fully extensible object-oriented architecture that supports subclassing, delegation, and a thorough set of notifications enabling deep customization.

related question 的答案提到了几个类,例如 NSTextStorageNSLayoutManagerNSTextContainer

考虑 UILabel 使用上述所有类来提供最终用户在屏幕上看到的父级 UIView 中显示文本的最终结果。布局管理器(NSLayoutManager 的实例)协调 TextView 、文本容器和文本存储之间的数据流,从而在 View 中显示字符。布局管理器将字符映射到字形,并确定使用哪些行来布置字形。布局管理器还弄清楚如何显示下划线和删除线等不属于字形的内容。

这个讨论的重点是布局管理器布置文本的。如果该行文本是可选择的,则用户可以选择该行中的可见字符。在这种特殊情况下,该行有“字符串访问”。

类似的概念是相关问题的解决方案中发布的方法:

func boundingRect(forGlyphRange glyphRange: NSRange, in container: NSTextContainer) -> CGRect

Returns a single bounding rectangle (in container coordinates) enclosing all glyphs and other marks drawn in the given text container for the given glyph range, including glyphs that draw outside their line fragment rectangles and text attributes such as underlining.

最后,函数 CTLineGetOffsetForStringIndex 的引用讨论谈到了适用于绘制自定义插入符号的图形偏移。插入符号可用于显示插入点或文本选择。主要和次要偏移量可以被认为是字符串的开始和结束索引——一个字符序列。如果给定行没有字符序列,则不会有选定的字符、插入符号和字形范围。因此没有“字符串访问”。

关于iphone - CTLine 有 "string access"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19441391/

相关文章:

ios - 向 UINavigationItem 添加按钮根本不起作用

ios - 调整 UITableViewCell 中的 UILabel 显示错误

iphone - UIView 子类不会自动调整大小

ios - UIImagePickerController 显示的 VIDEO_TOO_LONG_TITLE 警报

iphone - 我想从短信中打开我的应用程序

ios - 在 AVPlayerLayer 上拉伸(stretch)视频

ios - 分配给可选时意外发现 nil

iphone - sizeToFit 运行异常

iphone - 多个操作系统版本上的 iPhone 应用程序的最佳实践

iphone - 构建 View 层次结构的最佳实践