ios – 确定UITextView将截断字符串的位置

前端之家收集整理的这篇文章主要介绍了ios – 确定UITextView将截断字符串的位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个带长字符串的UITextView.文本视图大小由autolayout确定,并设置为tail截断文本.它看起来像这样:

UITextView truncating

如何确定显示的字符串范围和截断的范围?

解决方法

试试这个

class ViewController: UIViewController,NSLayoutManagerDelegate{

@IBOutlet weak var textView: UITextView!
var range :NSRange!
override func viewDidLoad() {
    super.viewDidLoad()


    textView.textContainer.layoutManager?.delegate = self
    let str = textView.text as NSString
    //Regardless of the repeated string
    range = str.rangeOfString("cillium adipisicing")
    print(range)


}

// Invoked while determining the soft line break point.  When NO,NSLayoutManager tries to find the next line break opportunity before charIndex
func layoutManager(layoutManager: NSLayoutManager,shouldBreakLineByWordBeforeCharacterAtIndex charIndex: Int) -> Bool{

    // charIndex is in the string or not
    if range.location != NSNotFound && NSLocationInRange(charIndex,NSRange(location: range.location + 1,length: range.length - 1 )) {
        print("range of string is truncated -> charIndex is \(charIndex)")
    }
    return  true
}
}

但是如果单词没有“”且截断,则charIndex为0,则仅限于BreakLineByWord

希望它有用:)

猜你在找的iOS相关文章