1
我從代碼設置屬性文本。 infoLabel
在具有字體系列和大小的故事板中配置。 infoLabel
包含3行文本。UILabel不顯示屬性文本
這是應該的樣子:
什麼代碼產生:
let daysCount: Int = 78
let weight: Double = -3.8
let fitness: Int = 44 // Percent
var daysWord = NSAttributedString(string: NSLocalizedString("days", comment: "Comparison label line 1"))
if daysCount == 1 {
daysWord = NSAttributedString(string: NSLocalizedString("day", comment: "Comparison label line 1"))
}
let firstLine = NSAttributedString(string: "\(daysCount) \(daysWord)")
let weightString = NSLocalizedString("\(weight) kg", comment: "Comparison label line 2")
let weightAttributes: [String : Any] = [NSForegroundColorAttributeName : UIColor.geGreenyBlue]
let attrubutedWeight = NSAttributedString(string: weightString, attributes: weightAttributes)
let secondLine = NSAttributedString(string: NSLocalizedString("weight: \(attrubutedWeight)", comment: "Comparison label line 2"))
let thirdLine = NSAttributedString(string: "")
infoLabel.attributedText = NSAttributedString(string: "\(firstLine)\n \(secondLine)\n \(thirdLine)")
當我在故事板設置文本與單一深灰色它看起來不錯,但是當我用代碼改變它時,它會在它上面添加大括號。
請檢查編輯答案一次 –
尼拉夫,你的意思是接受?不需要,即使您編輯它也會保持接受。感謝你的回答,顯然我需要做一些編輯,但是你解決了我的主要問題,我需要的只是調用'append()'。 –
@borisy我在說的是檢查編輯後的答案,因爲我在發佈答案後編輯了答案。所以它包含新的變化。不是關於接受的答案,因爲我知道你已經接受了我的答案。 –