我想在我的標籤中添加2個圖標。我有兩個圖像:一個是鳥,一個是鴨。如何在標籤中添加兩個NSTextAttachment?
我希望我的標籤顯示這樣的文字:
[鳥圖片]鳥[鴨圖片]鴨子。
目前,我只知道在一個標籤實現一個NSTextAttachment
。
let birdAttachment = NSTextAttachment()
let birdImage = UIImage(named:"bird")
birdAttachment.image = birdImage
let birdString = NSMutableAttributedString(string: "Bird")
let stringWithBirdImage = NSMutableAttributedString(attributedString: NSAttributedString(attachment: birdAttachment))
stringWithBirdImage.appendAttributedString(birdString)
let duckAttachment = NSTextAttachment()
let duckImage = UIImage(named: "duck")
duckAttachment.image = duckImage
let duckString = NSMutableAttributedString(string: "Duck")
let stringWithDuckImage = NSMutableAttributedString(attributedString: NSAttributedString(attachment: duckAttachment))
stringWithDuckImage.appendAttributedString(duckString)
label.attributedText = stringWithBirdImage
那麼如何在標籤中添加2個NSTextAttachment
。
他們都只是追加到'stringWithDuckImage'。 'stringWithBirdImage.appendAttributedString(stringWithDuckImage)''之前= label.attributedText只是stringWithBirdImage'? – Larme
謝謝。 @Larme。有效。 – Khuong
@Larme,你應該把答案放在一個答案中,以便你可以獲得信用。 – NRitH