ios - 如何使用 NSAttributedString 在 Swift 中将图像添加为文本附件?

标签 ios iphone swift ios-app-extension custom-keyboard

我正在尝试使用我作为按钮放入的图像为 iOS 构建自定义键盘。当我按下一个按钮时,链接到该按钮的图像被放入一个属性字符串中,该字符串被加载到自定义键盘 View 内的 UiTextView 中。这是有效的。

问题是,当我将新图像附加到属性字符串时,字符串中的旧图像和新图像都会更改为我当前按下的图像。我不明白为什么字符串中的旧图像会发生变化。

有什么建议吗?我试过使用 replaceCharactersInRangeinsertAttributedString 但无法正常工作。这是代码(在 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;
}

最佳答案

We cannot simply append NSTextAttachment image .We have to store all the attached Image and concatenation to existing attributed String.

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
                }
            }
        }
    }

}

enter image description here enter image description here

Demo Reference

关于ios - 如何使用 NSAttributedString 在 Swift 中将图像添加为文本附件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32772007/

相关文章:

swift - Xcode 10 iOS 应用程序构建, "hidden"任务占用了大部分构建时间

python - 如何加载测试基于客户端/服务器的 redis、twisted 和 iPhone 应用程序

iphone - UITableview,其中包含按排序顺序排列的名称部分

ios - UITableView 不显示内容,除非滚动或触摸单元格

ios - 如何从没有扩展名的文件加载图像到 UIImage

ios - UITableView 重新加载问题

ios - 如何在 Xcode 中模拟一个小的重复出现、重叠的位置路径?

ios - 在 RLMArray 上添加或更新对象?

ios - 如何在 Storyboard上隐藏pickerview?

ios - 如何比较两个数字并选择最小的继续执行?