14

NSTextAttachment を使用して UILabels にインラインで画像を追加する方法については、次の投稿に従いました。私はできる限り最善を尽くし、Swift で自分のバージョンを書きました。

チャット アプリケーションを作成していますが、ビール アイコンを挿入するフィールドで画像が表示されないか、画像がインラインで表示されないようです。エラーは発生しないので、コードに小さな詳細が欠けていると思います。

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;
4

4 に答える 4

36

これは UITextField では機能しません。UILabel でのみ機能します。

これは、コードに基づく UILabel 拡張機能です (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
    }
}

編集:

これは、ラベルの前後にアイコンを追加できる新しいバージョンです。ラベルからアイコンを外す機能もあります

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
    }
}
于 2015-09-28T18:02:41.677 に答える
0

以下の拡張機能を使用すると、確実に機能します

 extension NSMutableAttributedString { 
   static func addImageInBetweenString(firstSentence: String, image: UIImage?, lastSentence: String) -> NSMutableAttributedString {
     // create an NSMutableAttributedString that we'll append everything to
     let fullString = NSMutableAttributedString(string: firstSentence)

     // create our NSTextAttachment
     let image1Attachment = NSTextAttachment()
     image1Attachment.image = image
     //image1Attachment.setImageHeight(height: 15.0)
     Image1Attachment.bounds = CGRect(x: 0, y: -5, width: 15, height: 15)

     // wrap the attachment in its own attributed string so we can append it
     let image1String = NSAttributedString(attachment: image1Attachment)

    // add the NSTextAttachment wrapper to our full string, then add some more text.
    fullString.append(image1String)
    fullString.append(NSAttributedString(string: lastSentence))

    return fullString
   }
} 



extension NSTextAttachment {
   func setImageHeight(height: CGFloat) {
      guard let image = image else { return }
      let ratio = image.size.width / image.size.height
      bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: ratio * height, height: height)
  }
 }
于 2020-10-20T19:37:13.560 に答える