如何删除 NSTextAttachment 中的默认图像图标?

How to remove default image icon in NSTextAttachment?

我有一个可以包含 NSTextAttachments 的 NSAttributedString。我已经将 NSTextAttachment 子类化,并且有一个 属性 如果下载附件图像出现问题,它会存储一条错误消息。

我还将 NSLayoutManager 子类化以枚举附件,如果附件中存在错误,它会在我默认提供的 CGRect 中间的屏幕上显示错误消息。在模拟器中,这按计划工作;但是,在设备上,CGRect 的边界上有一个默认图标。请查看所附图片以了解此默认图标。 如何覆盖或删除此默认图标?

Example of the results in the simulator.

Example of the results on a device.

我已经研究了如何覆盖正在显示的这个图标,但没有找到任何东西。有什么想法吗?

我在 NSTextAttachment 子类中的初始化器非常基础:


    override init(data contentData: Data?, ofType uti: String?) {
        super.init(data: contentData, ofType: uti)
    }

    required convenience init?(coder: NSCoder) {
        self.init()
        imageCloudID = (coder.decodeObject(forKey: "imageLocation") as! String)
        setImage()
    }
    public override func encode(with coder: NSCoder) {
        coder.encode(imageCloudID, forKey: "imageLocation")
    }

然后如果下载图片出错,它会将可选错误 属性 设置为该错误。

NSLayoutManager 中,这是我的代码:

        // Enumerate attachments and draw a placeholder rectangle with error message if an error exists.
        textStorage.enumerateAttribute(.attachment, in: textStorage.entireRange, options: []) { optValue, range, stop in
            guard let attachment = optValue as? MyImageAttachment else { return }
            
            let glyphRange = self.glyphRange(forCharacterRange: range, actualCharacterRange: nil)
            self.enumerateLineFragments(forGlyphRange: glyphRange) { rect, usedRect, textContainer, suppliedGlyphRange, stop in
                // Get the size of the rectangle we want to show for the attachment placeholder.
                let width = rect.size.width
                let imageRect = CGRect(x: rect.minX + 5, y: rect.minY + 10, width: width - 10, height: width / 2)
                
                <<Create a bezier path to outline the rectangle.>>
                
                // Set the UI message to the error message, if present.
                guard let imageError = attachment.imageError else {
                    // If there is not an error present, exit the function.
                    return
                }
                let errorMessage: String
                switch imageError {
                case .no_Internet:
                    errorMessage = "Internet not available."
                << other error messages >>
                }

                // Show the error message in the placeholder rectangle.
                let messageFont = UIFont.systemFont(ofSize: 16, weight: .light)
                let attributedMessage = NSAttributedString(string: errorMessage, attributes: [NSAttributedString.Key.font: messageFont])
                // Center the message.
                let messageWidth = attributedMessage.size().width
                let startingPoint = (imageRect.width - messageWidth) / 2
                attributedMessage.draw(in: CGRect(x: startingPoint, y: imageRect.midY, width: imageRect.width, height: imageRect.height))
                return
            }
        }

最近,我遇到了类似的问题。在 NSTextAttachment 中为图像分配 nil 对我不起作用。

这就是我实施的解决方法。

        let attachment = NSTextAttachment()
        attachment.image = UIImage()
        return attachment