2014-11-04 13 views
5

Ho seguito il following post su come utilizzare NSTextAttachment per aggiungere immagini in linea con i vostri UILabels. Ho seguito il meglio che potevo e ho scritto la mia versione in Swift.Come inserire un'immagine UILabel in linea in iOS 8 usando swift

Sto creando un'applicazione di chat e il campo in cui inserisco un'icona di birra non esegue il rendering dell'immagine o non sembra rendere l'immagine in linea. Non ho alcun errore quindi presumo che mi manchi qualche piccolo dettaglio nel mio codice.

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

Qual è la dimensione dell'immagine? – Larme

risposta

16

Questo non funziona su un UITextField. Funziona solo su un UILabel.

Ecco un'estensione UILabel in base al codice (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:

qui è una nuova versione che permette di aggiungere l'icona prima o dopo l'etichetta. C'è anche una funzione per rimuovere l'icona dall'etichetta

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

ringraziamento Mate hai rocce –

+0

Se si prevede di aggiungere più immagini, ricordarsi di cambiare l'Initializer da lasciare strLabelText: NSAttributedString = NSAttributedString (stringa:! Self.text) a: lasciare strLabelText: NSMutableAttributedString = NSMutableAttributedString (attributedString: self.attributedText!) –

5

risposta estensione di Regis St-Gelais per Swift 3 e Swift 4 e senza scartare forzata:

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

Usage:

self.theLabel.text = "desiredText" 
self.theLabel.addImageWith(name: "nameOfImage", behindText: false) 
Problemi correlati