【问题标题】:How to remove default image icon in NSTextAttachment?如何删除 NSTextAttachment 中的默认图像图标?
【发布时间】:2021-08-26 03:30:50
【问题描述】:

我有一个可以包含 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
            }
        }

【问题讨论】:

    标签: swift nstextattachment nslayoutmanager


    【解决方案1】:

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

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

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

    【讨论】:

    • 好主意,简单有效。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    相关资源
    最近更新 更多