2015-09-24 25 views
8

Düğmeler olarak koyduğum görüntüleri kullanarak iOS için özel bir klavye oluşturmaya çalışıyorum. Bir düğmeye bastığımda, düğmeye bağlı görüntü, özel klavye görünümünde bir UiTextView'e yüklenen bir öznitelik dizesine konur. Bu çalışıyor.Görüntü eki olarak metin eki olarak nasıl eklenir nsattributedstring

Sorun, öznitelikli dizeye yeni bir resim eklediğimde, dizgedeki eski ve yeni görüntülerin, şu anda üzerine bastığım resme dönüştüğü. Dizedeki eski görüntülerin neden değiştiğini anlayamıyorum.

Herhangi bir öneriniz var mı? ReplaceCharactersInRange ve insertAttributedString kullanarak denedim ama işe alamıyorum. İşte kod (viewDidLoad sonra):

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

Teşekkür ederiz!

cevap

0

Sorunu buldum, NSTextAttachment için farklı değişkenlere ihtiyacım var (örneğin, textAttachment1, textAttachment2 vb.) Aksi halde yalnızca ilk görüntülenen resmi kullanır.

0

Bu benim için çalışıyor:

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

Biz sadece NSTextAttachment görüntüyü ekleme olamaz .Biz Dize atfedilen mevcut üzere tüm ekli Resim ve birleştirme saklamak zorunda.

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

Ben ekli 2 farklı resmi varsa , nasıl hangisinin im tıklayarak tanıyabileceği? Görüntü eki tıklandığında bir şey yazdırmak için kod yaptım, ancak görüntüye bağlı olarak farklı eylemler gerçekleştirmem gerekiyor. –

+0

Ayrıca, video eklemek mümkün mü? –