edString是否包含NSTextAttachment并将其删

edString是否包含NSTextAttachment并将其删

本文介绍了如何检测NSAttributedString是否包含NSTextAttachment并将其删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到 NSAttributedString 作为输入,其中可能包含附加为 NSTextAttachment 的图像。我需要检查实际上是否附加了这样的图像,在这种情况下,将其删除。我一直在寻找没有成功的相关帖子,我怎么能这样做?

I receive as an input a NSAttributedString that may contain an image attached as NSTextAttachment. I need to check if actually such image is attached and, in such case, remove it. I've been looking for related posts with no success, how could I do this?

编辑:我正在尝试这个:

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: ""))
            }
        }

如果 textView.attributedText 包含多个附件(我看到几个 \ u {ef} 字符串中),我希望枚举符合条件 if(值为?NSTextAttachment)!= nil 好几次,但那段代码只是exec一次。

If the textView.attributedText contains more than one attachment (I see several \u{ef} in its string), I expected the enumeration to match the condition if (value as? NSTextAttachment) != nil several times but that block of code is only executed once.

如何删除所有附件?

推荐答案

Swift 4,XCode 9回答:

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)

}

这篇关于如何检测NSAttributedString是否包含NSTextAttachment并将其删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:36