2014-11-04 66 views
5

我跟隨following post瞭解如何使用NSTextAttachment添加圖像並與您的UILabels內聯。我盡我所能並在Swift中編寫了我的版本。如何在iOS 8中使用swift插入圖像Inline UILabel

我正在創建一個聊天應用程序,並且我將一個啤酒圖標插入的字段不會呈現圖像,或者似乎不會呈現圖像內聯。我沒有得到任何錯誤,所以我假設我錯過了我的代碼中的一些小的細節。

var beerName:String! 

     if(sender == bn_beer1) 
     { 
      beerName = "beer1.png" 
     } 

     if(sender == bn_beer2) 
     { 
      beerName = "beer2.png" 
     } 

     if(sender == bn_beer3) 
     { 
      beerName = "beer3" 
     } 



     var attachment:NSTextAttachment = NSTextAttachment() 
     attachment.image = UIImage(named: beerName) 


     var attachmentString:NSAttributedString = NSAttributedString(attachment: attachment) 
     var myString:NSMutableAttributedString = NSMutableAttributedString(string: inputField.text) 
     myString.appendAttributedString(attachmentString) 



     inputField.attributedText = myString; 
+0

什麼是圖像大小? – Larme 2014-11-04 22:45:45

回答

16

這不適用於UITextField。它只適用於UILabel。

這是基於你的代碼一個UILabel擴展(雨燕2.0)

extension UILabel 
{ 
    func addImage(imageName: String) 
    { 
     let attachment:NSTextAttachment = NSTextAttachment() 
     attachment.image = UIImage(named: imageName) 

     let attachmentString:NSAttributedString = NSAttributedString(attachment: attachment) 
     let myString:NSMutableAttributedString = NSMutableAttributedString(string: self.text!) 
     myString.appendAttributedString(attachmentString) 

     self.attributedText = myString 
    } 
} 

編輯:

這裏是一個新版本,允許前或後的標籤添加圖標。也有從標籤中刪除圖標的功能

extension UILabel 
{ 
    func addImage(imageName: String, afterLabel bolAfterLabel: Bool = false) 
    { 
     let attachment: NSTextAttachment = NSTextAttachment() 
     attachment.image = UIImage(named: imageName) 
     let attachmentString: NSAttributedString = NSAttributedString(attachment: attachment) 

     if (bolAfterLabel) 
     { 
      let strLabelText: NSMutableAttributedString = NSMutableAttributedString(string: self.text!) 
      strLabelText.appendAttributedString(attachmentString) 

      self.attributedText = strLabelText 
     } 
     else 
     { 
      let strLabelText: NSAttributedString = NSAttributedString(string: self.text!) 
      let mutableAttachmentString: NSMutableAttributedString = NSMutableAttributedString(attributedString: attachmentString) 
      mutableAttachmentString.appendAttributedString(strLabelText) 

      self.attributedText = mutableAttachmentString 
     } 
    } 

    func removeImage() 
    { 
     let text = self.text 
     self.attributedText = nil 
     self.text = text 
    } 
} 
+2

謝謝配合你的岩石 – 2016-08-12 05:05:54

+0

如果您正在計劃添加多張圖片,記住要初始化從 改變讓strLabelText:NSAttributedString = NSAttributedString(字符串:self.text) 到: 讓strLabelText:NSMutableAttributedString = NSMutableAttributedString(attributedString: self.attributedText!) – 2017-02-28 09:24:18

5

雷吉斯聖Gelais的擴展答案斯威夫特3斯威夫特4且沒有強制解包:

extension UILabel { 

    func addImageWith(name: String, behindText: Bool) { 

     let attachment = NSTextAttachment() 
     attachment.image = UIImage(named: name) 
     let attachmentString = NSAttributedString(attachment: attachment) 

     guard let txt = self.text else { 
      return 
     } 

     if behindText { 
      let strLabelText = NSMutableAttributedString(string: txt) 
      strLabelText.append(attachmentString) 
      self.attributedText = strLabelText 
     } else { 
      let strLabelText = NSAttributedString(string: txt) 
      let mutableAttachmentString = NSMutableAttributedString(attributedString: attachmentString) 
      mutableAttachmentString.append(strLabelText) 
      self.attributedText = mutableAttachmentString 
     } 
    } 

    func removeImage() { 
     let text = self.text 
     self.attributedText = nil 
     self.text = text 
    } 
} 

用法:

self.theLabel.text = "desiredText" 
self.theLabel.addImageWith(name: "nameOfImage", behindText: false) 
相關問題