【问题标题】:Determine where UITextView will truncate string确定 UITextView 将截断字符串的位置
【发布时间】:2016-05-30 10:34:38
【问题描述】:

我有一个带有长字符串的UITextView。文本视图大小由自动布局确定,并设置为尾部截断文本。它看起来像这样:

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

【问题讨论】:

    标签: ios string swift autolayout uitextview


    【解决方案1】:

    试试这个

    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
    }
    }
    

    但仅限于BreakLineByWord,如果单词没有“”并被截断,charIndex is 0

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 2017-05-01
      • 2012-06-13
      • 1970-01-01
      • 2011-06-22
      • 2013-01-02
      • 1970-01-01
      • 2014-09-14
      • 2015-12-14
      相关资源
      最近更新 更多