简体   繁体   English

CTLine具有“字符串访问权”是什么意思?

[英]What does it mean for a CTLine to have “string access”?

I'm trying to solve a hairy problem with UILabel , and I've gotten most of it figured out, except for one thing: I'm having a challenge understanding what it means for a CTLine to have "string access". 我正试图解决UILabel的一个毛茸茸的问题 ,我已经弄明白了,除了一件事:我正在挑战,了解CTLine对“字符串访问”意味着什么。

The method that I'd like to use is CTLineGetOffsetForStringIndex . 我想使用的方法是CTLineGetOffsetForStringIndex Here's a link to the documentation for the method. 这是该方法文档的链接。

Here's the part of the documentation that I don't understand (emphasis is mine): 这是我不理解的文档部分(重点是我的):

The primary offset along the baseline for charIndex, or 0.0 if the line does not support string access. charIndex沿基线的主偏移量, 如果该行不支持字符串访问则为0.0。

When I'm running this method, I'm getting 0.0 back, so I guess that means the line doesn't support string access - but what does that mean, exactly? 当我运行这个方法时,我得到0.0 ,所以我猜这意味着该行不支持字符串访问 - 但这是什么意思,确切地说?

The statement "the line does not support string access" may be inferred as meaning that the line of text may not be treated as a sequence of characters that may be accessed by the index of each character. 声明“该行不支持字符串访问”可以被推断为意味着该行文本可以被视为可以由每个字符的索引访问的字符序列。

This may open up a large discussion about visual characters versus non-visual characters, and glyphs versus characters. 这可能会引发关于视觉角色与非视觉角色以及字形与角色的大讨论。 But to simplify the discussion, assume that a line of text may have one of the following states: 但为了简化讨论,假设一行文本可能具有以下状态之一:

  1. more than zero characters (characters which translate to either glyphs or whitespace within the same line) are present in the line of text in question 超过零个字符(转换为同一行内的字形或空格的字符)出现在相关文本行中
  2. there are no characters in the line of text which occupy any "space" 文本行中没有占据任何“空间”的字符

Now to provide some rationale for this inference. 现在为这种推断提供一些理由。

Apple's documentation provides a description of Text Kit, upon which UILabel is built: 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. UIKit框架包括几个类,其目的是在应用程序的用户界面中显示文本:UITextView,UITextField和UILabel,如在iOS中显示文本内容中所述。 Text views, created from the UITextView class, are meant to display large amounts of text. 从UITextView类创建的文本视图旨在显示大量文本。 Underlying UITextView is a powerful layout engine called Text Kit. 底层UITextView是一个强大的布局引擎,称为Text Kit。 If you need to customize the layout process or you need to intervene in that behavior, you can use Text Kit. 如果您需要自定义布局过程或需要干预该行为,则可以使用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是UIKit框架中的一组类和协议,提供高质量的印刷服务,使应用程序能够存储,布局和显示具有精细排版的所有特征的文本,例如字距调整,连字,换行和对齐。 Text Kit is built on top of Core Text, so it provides the same speed and power. Text Kit构建于Core Text之上,因此它提供相同的速度和功能。 UITextView is fully integrated with Text Kit; UITextView与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. 其他Text Kit类提供文本存储和布局功能。 Figure 8-1 shows the position of Text Kit among other iOS text and graphics frameworks. 图8-1显示了Text Kit在其他iOS文本和图形框架中的位置。

Figure 8-1 Text Kit Framework Position 图8-1文本工具包框架位置 图8-1文本工具包框架位置

Text Kit gives you complete control over text rendering in user interface elements. Text Kit使您可以完全控制用户界面元素中的文本呈现。 In addition to UITextView, UITextField and UILabel are built on top of Text Kit, and it seamlessly integrates with animations, UICollectionView and UITableView. 除了UITextView之外,UITextField和UILabel都构建在Text Kit之上,并且它与动画,UICollectionView和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. Text Kit设计有完全可扩展的面向对象的体系结构,支持子类化,委派和一组完整的通知,支持深度自定义。

The answer to the related question mentions several classes such as NSTextStorage , NSLayoutManager , and NSTextContainer . 相关问题的答案提到了几个类,如NSTextStorageNSLayoutManagerNSTextContainer

Consider that the UILabel uses all the above classes to provide the end result of displaying text in the parent UIView , which the end user sees on the screen. 考虑到UILabel使用上述所有类来提供在父UIView中显示文本的最终结果,最终用户在屏幕上看到该文本。 A layout manager (an instance of NSLayoutManager ) coordinates data flow between the text view, the text container, and the text storage, resulting in the display of characters in the view. 布局管理器( NSLayoutManager的实例)协调文本视图,文本容器和文本存储之间的数据流,从而在视图中显示字符。 The layout manager maps the characters to glyphs, and figures out which lines to use to lay out the glyphs. 布局管理器将字符映射到字形,并确定用于布置字形的行。 The layout manager also figures out how to display things like underline and strikethrough, which are not part of the glyphs. 布局管理器还会计算出如何显示下划线和删除线等内容,这些内容不是字形的一部分。

Important to this discussion is the fact that the Layout Manager lays out lines of text. 对于此讨论重要的是布局管理器布置了文本 If that line of text is selectable, the user may select visible characters in the line. 如果该行文本是可选择的,则用户可以选择该行中的可见字符。 In this particular case, there is "string access" for the line. 在这种特殊情况下,线路有“字符串访问”。

A similar concept is the method posted in the solution to related question: 类似的概念是在相关问题的解决方案中发布的方法:

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. 返回单个边界矩形(在容器坐标中),其中包含给定文本容器中为给定字形范围绘制的所有字形和其他标记,包括在其线条片段矩形外部绘制的字形和文本属性(如下划线)。

Finally, the reference discussion for the function CTLineGetOffsetForStringIndex speaks about graphical offsets which are suitable for drawing custom carets. 最后,函数CTLineGetOffsetForStringIndex的参考讨论讲述了适用于绘制自定义CTLineGetOffsetForStringIndex图形偏移。 The carets may be used to show insertion points or text selection. 插入符号可用于显示插入点或文本选择。 The primary and secondary offsets may be thought of as beginning and end indices for a string -- a sequence of characters. 主要和次要偏移可以被认为是字符串的开始和结束索引 - 字符序列。 If there is no sequence of characters for a given line, there can be no selected characters, no carets, no range of glyphs. 如果给定行没有字符序列,则可能没有选定的字符,没有插入符号,没有字形范围。 Therefore no "string access". 因此没有“字符串访问”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM