2016-05-13 98 views
7

我使用ContextLabel解析@,#和URL。這是我找到的最佳解決方案,使其尺寸正確,不會影響性能。它首先在輸入處解析字符串,然後將其轉換爲NSAttributedString,然後將其分配給attributedText屬性UILabel。一切正常,除了尾巴截斷 - 這是非常不正確的(見下面的圖)UILabel中的NSAttributedString尾截斷

enter image description here

哪兒我開始挖 - 它是不是在屬性串錯的屬性?或標籤佈局問題?謝謝!

+0

你硬編碼這個標籤的寬度是多少? – Feroz

回答

14

我有這個問題,通過增加一個NSParagraphStyle指定所需線中斷模式固定它:

//assuming myString is an NSMutableAttributedString 
    let paragraphStyle = NSMutableParagraphStyle() 
    paragraphStyle.lineBreakMode = .byTruncatingTail 

    let range = NSRange(location: 0, length: myString.mutableString.length) 
    myString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: range) 

用於進一步參考見Word wrap for NSMutableAttributedString

0

以下內容也適用於任何使用AttributedText或普通文本。
確保AttributedText和字體設置成標籤後添加下面一行:

label.lineBreakMode = .byTruncatingTail