我正在尝试使用我作为按钮放入的图像为 iOS 构建一个自定义键盘。当我按下按钮时,链接到该按钮的图像将被放入属性字符串中,该字符串将加载到自定义键盘视图内的 UiTextView 中。那是有效的。
问题是,当我将新图像附加到属性字符串时,字符串中的旧图像和新图像都会更改为我当前按下的图像。我不明白为什么字符串中的旧图像正在改变。
有什么建议么?我尝试过使用replaceCharactersInRange
and insertAttributedString
但无法让它发挥作用。这是代码(在 viewDidLoad 之后):
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;
}
我们不能简单地追加 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
}
}
}
}
}
演示参考 https://github.com/shrawan2015/Application-Demo-StackOverFlow/tree/master/AddImageToAttributedString
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)