2017-02-13 58 views
1

我從代碼設置屬性文本。 infoLabel在具有字體系列和大小的故事板中配置。 infoLabel包含3行文本。UILabel不顯示屬性文本

這是應該的樣子:

Labels with 3 lines of text. Some parts of it coloured

什麼代碼產生:

Label with text: 78 days{}{}...

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)") 

當我在故事板設置文本與單一深灰色它看起來不錯,但是當我用代碼改變它時,它會在它上面添加大括號。

回答

1

你的代碼有很多問題。您還沒有正確設置NSAttributedString,最後您甚至沒有正確組合多個NSAttributedString。它應該是這樣的。

let daysCount: Int = 78 
let weight: Double = -3.8 
let fitness: Int = 44 // Percent 

var daysWord = "days" 
if daysCount == 1 { 
    daysWord = "day" 
} 

let firstLine = NSAttributedString(string: "\(daysCount) \(daysWord)") 

//For Weight 
let weightAttrStr = NSAttributedString(string: "weight: ") 
let weightAttributes: [String : Any] = [NSForegroundColorAttributeName : UIColor. geGreenyBlue] 
let attrubutedWeight = NSAttributedString(string: "\(weight) kg", attributes: weightAttributes) 

//For Fitness 
let fitnessAttrStr = NSAttributedString(string: "fitness: ") 
let fitnessAttributes: [String : Any] = [NSForegroundColorAttributeName : UIColor. geGreenyBlue] 
let attrubutedFitness = NSAttributedString(string: "+\(fitness)%", attributes: fitnessAttributes) 

//Now Combine all the attributed with \n 
let combineAttr = NSMutableAttributedString() 
combineAttr.append(firstLine) 
combineAttr.append(NSAttributedString(string: "\n")) 
combineAttr.append(weightAttrStr) 
combineAttr.append(attrubutedWeight) 

combineAttr.append(NSAttributedString(string: "\n")) 
combineAttr.append(fitnessAttrStr) 
combineAttr.append(attrubutedFitness) 

//Now set textAligment to center 
let paragraph = NSMutableParagraphStyle() 
paragraph.alignment = .center 
let attributes: [String : Any] = [NSParagraphStyleAttributeName: paragraph] 
combineAttr.addAttributes(attributes, range: NSRange(location: 0, length: combineAttr.string.characters.count)) 

//Now set the combine attributedString to label 
infoLabel.attributedText = combineAttr 
+0

請檢查編輯答案一次 –

+0

尼拉夫,你的意思是接受?不需要,即使您編輯它也會保持接受。感謝你的回答,顯然我需要做一些編輯,但是你解決了我的主要問題,我需要的只是調用'append()'。 –

+0

@borisy我在說的是檢查編輯後的答案,因爲我在發佈答案後編輯了答案。所以它包含新的變化。不是關於接受的答案,因爲我知道你已經接受了我的答案。 –