2015-09-24 13 views
8

का उपयोग कर स्विफ्ट में टेक्स्ट अटैचमेंट के रूप में छवियों को जोड़ने के लिए कैसे मैं छवियों का उपयोग करके आईओएस के लिए एक कस्टम कीबोर्ड बनाने की कोशिश कर रहा हूं जिसे मैंने बटन के रूप में रखा है। जब मैं एक बटन दबाता हूं, बटन से जुड़ी छवि को एक जिम्मेदार स्ट्रिंग में डाल दिया जाता है जिसे कस्टम कीबोर्ड दृश्य के अंदर UiTextView में लोड किया जाता है। वह काम कर रहा हैnsattributedstring

समस्या यह है कि जब मैं जिम्मेदार स्ट्रिंग में एक नई छवि जोड़ता हूं तो स्ट्रिंग में पुरानी और नई छवियां उस छवि में बदल रही हैं, जिसे मैंने वर्तमान में दबाया था। मैं समझ नहीं पा रहा हूं कि स्ट्रिंग में पुरानी छवियां क्यों बदल रही हैं।

कोई सुझाव? मैंने प्रतिस्थापनकर्ताओं का उपयोग करने की कोशिश की है और डालें एट्रिब्यूटेड स्ट्रिंग लेकिन इसे काम पर नहीं ला सकता है। यहां कोड है (देखेंडिडलोड के बाद):

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 छवि संलग्न नहीं कर सकते हम मौजूदा जिम्मेदार ठहराया स्ट्रिंग को सभी संलग्न छवि और संयोजन स्टोर करने के लिए किया है।

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

इसके अलावा, क्या एक वीडियो संलग्न करना संभव है? –

संबंधित मुद्दे