2017-01-08 4 views
0

Je reçois en entrée un NSAttributedString pouvant contenir une image attachée comme NSTextAttachment. Je dois vérifier si une telle image est attachée et, dans ce cas, l'enlever. J'ai cherché des publications liées sans succès, comment pourrais-je faire ceci?Comment détecter si un NSAttributedString contient un NSTextAttachment et le supprimer?

EDIT: J'essaie ceci:

let mutableAttrStr = NSMutableAttributedString(attributedString: textView.attributedText) 
textView.attributedText.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0, textView.attributedText.length), options: NSAttributedString.EnumerationOptions(rawValue: 0)) { (value, range, stop) in 

      if (value as? NSTextAttachment) != nil { 
       mutableAttrStr.replaceCharacters(in: range, with: NSAttributedString(string: "")) 
      } 
     } 

Si le textView.attributedText contient plus d'une pièce jointe (je vois plusieurs \u{ef} dans son string), je me attendais l'énumération pour correspondre à la condition if (value as? NSTextAttachment) != nil plusieurs fois mais ce bloc de code n'est exécuté qu'une seule fois.

Comment puis-je supprimer toutes les pièces jointes?

+0

Pourriez-vous rechercher et supprimer tous les caractères de fixation: https://developer.apple.com/reference/uikit/nstextattachment/1508411-attachment_character – MathewS

+1

énumèrent les attributeString pour 'NSAttachmentAttributeName', et supprimez-les. Voici un code que vous pouvez regarder: http://stackoverflow.com/questions/29152660/extract-uiimage-from-nsattributed-string/29153172#29153172 – Larme

+0

@MathewS merci, et quelle est la façon la plus appropriée d'énumérer les caractères d'un ' NSAttributedString' pour vérifier si 'NSAttachmentCharacter' devrait être? – AppsDev

Répondre

0

Swift 4, XCode 9 Réponse:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 

    picker.dismiss(animated: true, completion: nil) 

    guard let image = info["UIImagePickerControllerOriginalImage"] as? UIImage else { 
     return 
    } 

//check if textview contains any attachment  

txtView.attributedText.enumerateAttribute(NSAttributedStringKey.attachment, in: NSRange(location: 0, length: txtView.attributedText.length), options: []) { (value, range, stop) in 

     if (value is NSTextAttachment){ 
      let attachment: NSTextAttachment? = (value as? NSTextAttachment) 

      if ((attachment?.image) != nil) { 
       print("1 image attached") 
       let mutableAttr = txtView.attributedText.mutableCopy() as! NSMutableAttributedString 
       //Remove the attachment 
       mutableAttr.replaceCharacters(in: range, with: "") 
       txtView.attributedText = mutableAttr 

      }else{ 
       print("No image attched") 
      } 
     } 
    } 
    //insert only one selected image into TextView at the end 
    let attachment = NSTextAttachment() 
    attachment.image = image 
    let newWidth = txtView.bounds.width - 20 
    let scale = newWidth/image.size.width 

    let newHeight = image.size.height * scale 
    attachment.bounds = CGRect.init(x: 0, y: 0, width: newWidth, height: newHeight) 
    let attrString = NSAttributedString(attachment: attachment) 
    txtView.textStorage.insert(attrString, at: txtView.selectedRange.location) 

}