2015-09-24 9 views
8

Ich versuche, eine benutzerdefinierte Tastatur für iOS zu erstellen, die Bilder verwendet, die ich als Schaltflächen eingefügt habe. Wenn ich eine Taste drücke, wird das Bild, das mit der Schaltfläche verknüpft ist, in eine attributierte Zeichenfolge eingefügt, die in eine UiTextView in der benutzerdefinierten Tastaturansicht geladen wird. Das funktioniert.Hinzufügen von Bildern als Textanhang in Swift mit nsattributedstring

Das Problem ist, dass, wenn ich ein neues Bild an die attributierte Zeichenfolge anfügen, die alten und neuen Bilder in der Zeichenfolge zu dem Bild wechseln, das ich gerade gedrückt habe. Ich kann nicht verstehen, warum sich die alten Bilder in der Zeichenkette ändern.

Irgendwelche Vorschläge? Ich habe versucht, replaceCharactersInRange und insertAttributedString, aber es kann nicht funktionieren. Hier ist der Code (nach 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; 
    } 

Vielen Dank!

Antwort

0

Ich habe das Problem gefunden, ich muss verschiedene Variablen für die NSTextAttachment (dh. TextAttachment1, TextAttachment2 etc.) haben, sonst wird nur das erste Bild verwendet, das adressiert wurde.

0

Dies funktioniert für mich:

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

Wir können nicht einfach anhängen NSTextAttachment Bild .Wir haben speichern alle beigefügten Bild und Verkettung String zugeschrieben bestehenden.

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

Wenn ich 2 verschiedene Bilder angehängt haben, wie konnte ich erkennen, die man im Klick? Ich habe den Code erstellt, um etwas zu drucken, wenn ein Bildanhang angeklickt wird, aber ich muss verschiedene Aktionen abhängig vom Bild realisieren. –

+0

Ist es auch möglich, ein Video anzuhängen? –

Verwandte Themen