简体   繁体   English

如果内容超出Swift中的数字行限制,请分割文本

[英]Split text if its content exceeds numbers line limit in Swift

Conditions: 条件:

  • UITextView that can contains X lines as maximum:disable scroll. 最多可以包含X行的UITextView:禁用滚动。 (For example; max lines = 8) (例如,最大行数= 8)
  • A huge text content. 大量的文字内容。 (For example; 25 lines) (例如; 25行)
  • Language is English. 语言是英语。 (Word-wrap is compulsory) (自动换行是强制性的)

How can I split text if its content exceeds numbers line limit? 如果内容超过数字行数限制,如何分割文本? I want to split text into array. 我想将文本拆分为数组。 Following the condition, the text should be split to 4 elements. 根据条件,文本应分为4个元素。

Update: Actually what I want to achieve is like when I have a long text and I have the fix text container size (eg. UITextView) which cannot display all content, then there is a next button to reset container and display the left content. 更新:实际上,我想要达到的效果是,当我有一个长文本并且具有固定文本容器大小(例如UITextView)时,它无法显示所有内容,然后有一个下一步按钮可以重置容器并显示剩余内容。 I thought that split string would help but it seems a wrong choice. 我以为分割字符串会有所帮助,但这似乎是错误的选择。 I am trying what Matt's suggestion and hope the problem will be solved. 我正在尝试Matt的建议,希望该问题能够得到解决。

Your goal is not a good one. 您的目标不是一个好目标。 Instead of splitting the text into four arrays of text, it would be better to take advantage of Text Kit and let the layout manager split the text into four text containers (one NSLayoutManager, four NSTextContainers). 与其将文本分割成四个文本数组,不如利用文本工具包,让布局管理器将文本分割成四个文本容器(一个NSLayoutManager,四个NSTextContainers)会更好。

This could even be combined with your current use of a text view for display purposes: the four text containers could belong to four text views, and now you have four noneditable nonscrollable text views that automatically distribute the text between them, and you can just switch among those text views. 甚至可以将其与当前用于显示目的的文本视图结合使用:四个文本容器可以属于四个文本视图,现在您具有四个不可编辑的不可滚动文本视图,它们可以在它们之间自动分配文本,您可以切换在这些文本视图中。

Here's an example with just two text views: 这是一个只有两个文本视图的示例:

在此处输入图片说明

That's two UITextView objects governed by one NSLayoutManager, so that as the text becomes too long for the first text view it automatically flows into the second text view. 那是由一个 NSLayoutManager控制的两个UITextView对象,因此当文本对于第一个文本视图来说太长时,它会自动流入第二个文本视图。

If you really wanted to, I suppose you could do what I just said and then use the layout manager to ask what it did, ie to read backwards from the line fragments to the glyphs to the character ranges and find out how it split the text into four text containers; 如果您确实想要,我想您可以按照我说的做, 然后使用布局管理器询问它做了什么,即从行片段向后读到字形再到字符范围,然后找出如何拆分文本放入四个文本容器中; but this seems silly when you consider that the problem is already solved before you even start to do that. 但是当您认为问题甚至在开始解决之前就已经解决了,这似乎很愚蠢。

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

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