简体   繁体   English

使用maximumNumberOfLines获取UITextView中的最后一个可见单词!= 0

[英]Get last visible word in UITextView with maximumNumberOfLines != 0

I have a UITextView that has a maximum number of lines 3 : textView.textContainer.maximumNumberOfLines = 3 我有一个UITextView,其最大行数为3: textView.textContainer.maximumNumberOfLines = 3

The text is longer than 3 lines, and I would like that the last word visible would be more . 文本超过3行,我希望可见的最后一个单词更多

Eg : For the string : 例如:对于字符串:

This string is pretty long and we would like to be able to identify the last word visible so we can display "more" instead of the word found. 这个字符串非常长,我们希望能够识别可见的最后一个单词,这样我们就可以显示“更多”而不是找到的单词。

For 3 lines, the text displayed is : 对于3行,显示的文本是:

This string is pretty long and we would like to be able to identify 这个字符串很长,我们希望能够识别

The result that I'm looking for : 结果我正在寻找:

This string is pretty long and we would like to be able to more 这个字符串很长,我们希望能够做到更多

Is there any API in UITextView, or in the text container that would allow me to get that last word displayed? UITextView中是否有任何API,或者在文本容器中是否允许我显示最后一个单词?

I would suggest you the ilyapuchka ReadMoreTextView library which is on this link . 我建议你在这个链接上的ilyapuchka ReadMoreTextView库。 It is the easiest way to achieve this and it is pretty lightweight. 这是实现这一目标的最简单方法,它非常轻巧。

You can install it with CocoaPods, just implement pod 'ReadMoreTextView' into the podfile and you can use it like this: 您可以使用CocoaPods安装它,只需将pod“ReadMoreTextView”实现到podfile中,您可以像这样使用它:

let textView = ReadMoreTextView()

textView.text = "Lorem ipsum dolor ..."

textView.shouldTrim = true
textView.maximumNumberOfLines = 3
textView.trimText = "Read more"

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

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