2016-06-24 6 views
-1

私のラベルに2つのアイコンを追加したいと思います。私は2つの画像を持っています:1つは鳥で、もう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' 'にそれらすべてを追加します。'label.attributedText = stringWithBirdImage'の直前に' stringWithBirdImage.appendAttributedString(stringWithDuckImage) 'がありますか? – 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

これは明らかです: 'let finalAttributedString = NSMutableAttributedString(string:" "); finalAttributedString.appendAttributedString(birdString); finalAttributedString.appendAttributedString(stringWithBirdImage); finalAttributedString.appendAttributedString(duckString); finalAttributedString.appendAttributedString(stringWithDuckImage); label.attributedText = finalAttributedString' また、かなりの数の "mutable"属性の文字列を持つことは無用です。 – 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

関連する問題