2015-09-24 17 views
8

私はボタンとして入れた画像を使ってiOSのカスタムキーボードを構築しようとしています。ボタンを押すと、ボタンにリンクされている画像が、カスタムキーボードビュー内のUiTextViewにロードされる属性付き文字列に入れられます。それは働いている。nsattributedstringを使ってSwiftに画像をテキストとして添付する方法

問題は、アトリビュートされた文字列に新しいイメージを追加すると、文字列内の古いイメージと新しいイメージの両方が、現在押されているイメージに変わるということです。私は文字列の古いイメージが変化している理由を理解できません。

提案がありますか?私はreplaceCharactersInRangeとinsertAttributedStringを使ってみましたが、動作させることはできません。ここでは、コード(のviewDidLoad後)である:

let textAttachment = NSTextAttachment() 

let textView = UITextView(frame: CGRectMake(5, 5, 200, 40)) 
var attributedString = NSMutableAttributedString(string: "") 

@IBAction func buttonPressed(button :UIButton) { 

    let string = button.titleLabel?.text 

    textAttachment.image = UIImage(named: "\(string!).png")! 
    textAttachment.image = UIImage(CGImage: textAttachment.image!.CGImage!, scale: 6, orientation: .Up) 
    let attrStringWithImage = NSAttributedString(attachment: textAttachment) 
    attributedString.appendAttributedString(attrStringWithImage); 


    textView.attributedText = attributedString; 
    } 

ありがとうございました!

答えて

0

問題が見つかりました.NSTextAttachment(つまりtextAttachment1、textAttachment2など)の変数が異なる必要があります。それ以外の場合は、最初にアドレス指定されたイメージが使用されます。

0

これが私の作品:

let attributedStringTextAttachment = NSTextAttachment() 
attributedStringTextAttachment.image = UIImage(named: "image") 
3

私たちは、単にがNSTextAttachmentイメージ我々を追加することはできません既存の帰属 Stringに 接続されているすべての画像および連結を保存する必要があります。

func buttonPressed(_ sender: Any) { 

    let button = sender as! UIButton 
    let tag = button.tag 
    let attributedString:NSAttributedString! 

    switch tag { 
    case 2: 
     attributedString = addAttributedText(text: (button.titleLabel?.text)!) 
    case 3: 
     attributedString = addAttributedText(text: (button.titleLabel?.text)!) 
    case 4: 
     attributedString = addAttributedText(text: (button.titleLabel?.text)!) 
    default: 
     attributedString = addAttributedText(text: "launch") 
     } 
     textView.attributedText = attributedString 
    } 

    func addAttributedText(text:String) -> NSAttributedString { 
     textViewDidChange(textView) 
     let axtractedImageAttribute = NSMutableAttributedString() 
     for image in imageArray { 
      let attachment:NSTextAttachment = NSTextAttachment() 
      attachment.image = image 
      attachment.setImageHeight(height: 20) 
      let attachmentString:NSAttributedString = NSAttributedString(attachment: attachment) 
      axtractedImageAttribute.append(attachmentString) 
     } 

     let attachment:NSTextAttachment = NSTextAttachment() 
     attachment.image = UIImage(named: "\(text).png") 
     attachment.setImageHeight(height: 20) 
     let attachmentString:NSAttributedString = NSAttributedString(attachment: attachment) 
     let attributedString:NSMutableAttributedString = NSMutableAttributedString(string:textView.text!) 
     attributedString.append(axtractedImageAttribute) 
     attributedString.append(attachmentString) 
     return attributedString 
    } 

    //MARKS:- Extract attachedImage 
    func textViewDidChange(_ textView: UITextView) { 
     imageArray = [UIImage]() 
     let range = NSRange(location: 0, length: textView.attributedText.length) 
     if (textView.textStorage.containsAttachments(in: range)) { 
      let attrString = textView.attributedText 
      var location = 0 
      while location < range.length { 
       var r = NSRange() 
       let attrDictionary = attrString?.attributes(at: location, effectiveRange: &r) 
       if attrDictionary != nil { 
        let attachment = attrDictionary![NSAttachmentAttributeName] as? NSTextAttachment 
        if attachment != nil { 
         if attachment!.image != nil { 
          imageArray.append(attachment!.image!) 
         } 
        } 
        location += r.length 
       } 
      } 
     } 
    } 

} 

enter image description here enter image description here

Demo Reference

+0

私は付属の2個の異なるイメージがある場合、どのように私はどちらのIMクリックを認識することができますか?画像の添付ファイルをクリックしたときに何かを印刷するコードを作ったが、画像に応じて異なる動作を実現する必要がある。 –

+0

また、ビデオを添付することはできますか? –

関連する問題