gpt4 book ai didi

swift - 将包含图像的 NSTextAttachment 替换为字符串

转载 作者:行者123 更新时间:2023-11-28 06:14:40 24 4
gpt4 key购买 nike

之前,我将特定字符串更改为包含图像的 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 替换为字符串以发送消息。我使用了 NSMutableAttributedString.replaceCharacters(in:with:) 方法。但是,它只能使用一个表情符号图像。

one emoticon two emoticons or more

我该如何解决?

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

第二:将 NSTextAttachment 替换为 String 并计算更改的范围。 <- 重复

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

结果:result

完美!!

关于swift - 将包含图像的 NSTextAttachment 替换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45524664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com