2016-06-24 23 views
-1

我想在我的標籤中添加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

+1

他們都只是追加到'stringWithDuckImage'。 'stringWithBirdImage.appendAttributedString(stringWithDuckImage)''之前= label.attributedText只是stringWithBirdImage'? – Larme

+0

謝謝。 @Larme。有效。 – Khuong

+0

@Larme,你應該把答案放在一個答案中,以便你可以獲得信用。 – NRitH

回答

0

我跟着評論@Larme答案。

let birdAttachment = NSTextAttachment() 
let birdImage = UIImage(named:"bird") 
birdAttachment.image = birdImage 
let birdString = NSAttributedString(string: "Bird") 
let stringWithBirdImage = NSAttributedString(attributedString: NSAttributedString(attachment: birdAttachment)) 

let duckAttachment = NSTextAttachment() 
let duckImage = UIImage(named: "duck") 
duckAttachment.image = duckImage 
let duckString = NSAttributedString(string: "Duck") 
let stringWithDuckImage = NSAttributedString(attributedString: NSAttributedString(attachment: duckAttachment)) 

let finalAttributedString = NSMutableAttributedString(string: "") 
finalAttributedString.appendAttributedString(stringWithBirdImage) 
finalAttributedString.appendAttributedString(birdString) 
finalAttributedString.appendAttributedString(stringWithDuckImage) 
finalAttributedString.appendAttributedString(duckString) 
label.attributedText = finalAttributedString 

它運作良好。

enter image description here

+1

這可能是更清楚,要做到:'讓finalAttributedString = NSMutableAttributedString(字符串: 「」); finalAttributedString.appendAttributedString(birdString); finalAttributedString.appendAttributedString(stringWithBirdImage); finalAttributedString.appendAttributedString(duckString); finalAttributedString.appendAttributedString(stringWithDuckImage); label.attributedText = finalAttributedString' 同時它也沒用有那麼多的「可變」屬性串,當相當一些人不發生變異。 – Larme

+0

@Larme再次感謝。我編輯過它。 – Khuong

2

這裏有一個小的調整,以@Khuong和@ Larme的答案簡潔:

func stringForAttachment(named imageName: String, caption: String) -> NSAttributedString { 
    let attachment = NSTextAttachment() 
    let image = UIImage(named: imageName) 
    attachment.image = image 
    let fullString = NSMutableAttributedString(string: caption) 
    fullString.appendAttributedString(NSAttributedString(attachment: attachment)) 
    return fullString 
} 

let labelText = NSMutableAttributedString() 
labelText.appendAttributedString(stringForAttachment(named: "bird", caption: "Bird")) 
labelText.appendAttributedString(stringForAttachment(named: "duck", caption: "Duck")) 
label.attributedText = labelText 
+0

這對我有幫助:) +1。 – Khuong

+0

我在原始代碼中也沒有看到任何換行符。你想在哪裏,你可以在那裏附加一個'\ n'? – NRitH

+0

這個工程範圍內的UILabel和UITextView的顯示圖像,但我無法得到它的工作的UITextField有趣 – vikzilla

相關問題