2014-11-04 15 views
5

Tôi đã theo dõi following post về cách sử dụng NSTextAttachment để thêm hình ảnh nội tuyến với UILabels của bạn. Tôi đã làm theo những gì tốt nhất có thể và viết phiên bản của tôi trong Swift.Làm cách nào để chèn hình ảnh Inline UILabel trong iOS 8 bằng cách sử dụng nhanh

Tôi đang tạo ứng dụng trò chuyện và trường mà tôi chèn biểu tượng bia vào không hiển thị hình ảnh hoặc dường như không hiển thị nội dòng hình ảnh. Tôi không nhận được bất kỳ lỗi nào vì vậy tôi giả sử tôi thiếu một số chi tiết nhỏ nhỏ trong mã của tôi.

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

Kích thước hình ảnh là gì? – Larme

Trả lời

16

Điều này không hoạt động trên UITextField. Nó chỉ hoạt động trên một UILabel.

Đây là một phần mở rộng UILabel dựa trên mã của bạn (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:

đây là một phiên bản mới cho phép để thêm vào biểu tượng trước hoặc sau nhãn. Ngoài ra còn có một chức năng để loại bỏ các biểu tượng từ nhãn

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

cảm ơn bạn giao phối bạn đá –

+0

Nếu bạn đang lập kế hoạch về Thêm nhiều hình ảnh, Ghi để thay đổi Initializer từ hãy strLabelText: NSAttributedString = NSAttributedString (string: self.text) tới: hãy strLabelText: NSMutableAttributedString = NSMutableAttributedString (attributedString: self.attributedText!) –

5

Regis St-Gelais của câu trả lời phần mở rộng cho Swift 3Swift 4 và không unwrapping buộc:

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

Cách sử dụng:

self.theLabel.text = "desiredText" 
self.theLabel.addImageWith(name: "nameOfImage", behindText: false) 
Các vấn đề liên quan