Swift 替换包含图像到字符串的NSTextAttachment

Swift 替换包含图像到字符串的NSTextAttachment,swift,string,nsmutableattributedstring,nstextattachment,Swift,String,Nsmutableattributedstring,Nstextattachment,之前,我将包含图像的特定字符串更改为NSTextAttachment以显示自定义表情 NSTextAttachment代码的字符串 { guard let original = self.attributedText else { return } let pattern = "\\[img src=(\\w+)\\]" do{ let regex = try NSRegularExpression(pattern: pa

之前,我将包含图像的特定字符串更改为NSTextAttachment以显示自定义表情

NSTextAttachment代码的字符串

{
    guard
        let original = self.attributedText
        else { return }
    let pattern = "\\[img src=(\\w+)\\]"

    do{
        let regex = try NSRegularExpression(pattern: pattern, options: [])
        let matches = regex.matches(in: original.string, options : [], range : NSMakeRange(0, original.string.characters.count))
        let attributeString = NSMutableAttributedString(attributedString: original)

        for match in matches.reversed(){
            let emoticonString = attributeString.attributedSubstring(from: match.rangeAt(1)).string

            if  let emoticonAndroid = Emoticon(rawValue: emoticonString),
                let image = UIImage(named : "\(emoticonAndroid.convertFromAndroid().rawValue)_000"){
                image.accessibilityIdentifier = emoticonAndroid.rawValue
                let attributedImage = NSTextAttachment()
                attributedImage.image = image
                attributedImage.bounds = CGRect(x: 0, y: -8, width: 25, height: 25)
                attributeString.beginEditing()
                attributeString.replaceCharacters(in: match.rangeAt(0), with: NSAttributedString(attachment: attributedImage))
                attributeString.endEditing()
            }
        }

        self.attributedText = attributeString
    }catch{
        return
    }

}
但是,我需要将NSTextAttachment替换为字符串来发送消息。 我使用了nsmutableAttributeString.replaceCharacters(in:with:)方法。但是,它只能处理一个表情符号图像

我怎样才能修好它

NSTextAttachment到字符串代码

{
    if let original = self.attributedText{
        let attributeString = NSMutableAttributedString(attributedString: original)

        original.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0, original.length), options: [], using: { attribute, range, _ in
            if let attachment = attribute as? NSTextAttachment,
                let image = attachment.image{
                let str = "[img src=\(image.accessibilityIdentifier!)]"

                attributeString.beginEditing()
                attributeString.(in: range, with: str)
                attributeString.endEditing()
            }
        })

        self.attributedText = attributeString
        return attributeString.string
    }else{
        return nil
    }
}

嗯。。我解决了这个问题

第一:统计NSTextAttachment的数量

var count = 0
    self.attributedText.enumerateAttribute(NSAttachmentAttributeName, in : NSMakeRange(0, self.attributedText.length), options: [], using: { attribute, range, _ in
        if let attachment = attribute as? NSTextAttachment,
            let image = attachment.image{
            count = count + 1
        }
    })
    return count

第二:用字符串替换NSTEXTATTANCH并计算更改的范围。使用
选项:.reverse
避免for循环(不要弄乱范围),并跳过第一个代码段。
for i in 0..<self.countOfNSTextAttachment(){
        let attributedString = NSMutableAttributedString(attributedString: self.attributedText)
        var count = 0
        attributedString.enumerateAttribute(NSAttachmentAttributeName, in : NSMakeRange(0, attributedString.length), options: [], using: { attribute, range, _ in
            if let attachment = attribute as? NSTextAttachment,
                let image = attachment.image{
                let str = "[img src=\(image.accessibilityIdentifier!)]"

                if count == 0{
                    attributedString.beginEditing()
                    attributedString.replaceCharacters(in: range, with: NSAttributedString(string : str))
                    attributedString.endEditing()
                    self.attributedText = attributedString
                }else{
                    return
                }
                count = count + 1
            }
        })
    }

    return self.attributedText.string