2014-11-04 6 views
5

Połączyłem się z following post w sprawie korzystania z NSTextAttachment w celu dodawania obrazów w linii z twoimi UILabelami. Śledziłem wszystko, co mogłem i napisałem swoją wersję w Swift.Jak wstawić obraz Inline UILabel w systemie iOS 8 za pomocą Swift

Tworzę aplikację do czatu, a pole, w które wkładam ikonę piwa, nie powoduje renderowania obrazu lub nie renderuje obrazu w linii. Nie dostaję żadnych błędów, więc zakładam, że brakuje mi trochę małych szczegółów w moim kodzie.

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

Jaki jest rozmiar obrazu? – Larme

Odpowiedz

16

To nie działa na UITextField. Działa tylko na UILabel.

Oto przedłużenie UILabel na podstawie kodu (Swift 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 
    } 
} 

EDIT:

tutaj jest nowa wersja, która pozwoli, aby dodać ikonę przed lub po etykiecie. Jest też funkcja, aby usunąć ikonę z etykietą

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

dziękuję kolego jesteś skały –

+0

Jeśli planujesz na dodawanie wielu zdjęć, Pamiętaj, aby zmienić initializer z niech strLabelText: NSAttributedString = NSAttributedString (string: self.text) do: niech strLabelText: NSMutableAttributedString = NSMutableAttributedString (attributedString: self.attributedText!) –

5

Regis St-Gelais za przedłużenie odpowiedź na Swift 3 i Swift 4 i bez wymuszonej odpakowanie:

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 
    } 
} 

Zastosowanie:

self.theLabel.text = "desiredText" 
self.theLabel.addImageWith(name: "nameOfImage", behindText: false) 
Powiązane problemy