2017-02-13 16 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

Nirav、受け入れてもよろしいですか?あなたがそれを編集しても、受け入れられている必要はありません。あなたにお返事いただきありがとうございます。私はいくつかの編集を行う必要がありましたが、私の主な問題を解決しました。私が必要とするのは 'append()'を呼び出すことだけです。 –

+0

@borisy私が言っているのは、私が答えを投稿した後に編集された答えを編集したためです。したがって、新しい変更が含まれています。あなたがすでに私の答えを受け入れたことを知っているので、受け入れる答えではありません。 –

関連する問題