ios - 通过 NSAttributedStringKey 的附件不可见

标签 ios objective-c swift nstextattachment

可以通过 largeTitleTextAttributes 自定义新的大标题功能,(与任何其他属性一样) dictionary with NSAttributedStringKey 键。其中一个键是 NSAttachmentAttributeName/attachment

考虑一下:

let attachment = NSTextAttachment()
attachment.image = UIImage(named: "foo")
attachment.bounds = CGRect(x: 0.0, y: 0.0, width: 20.0, height: 20.0)

var largeTitleTextAttributes: [NSAttributedStringKey: Any] = [:]
largeTitleTextAttributes[.attachment] = attachment
navigationBar.largeTitleTextAttributes = largeTitleTextAttributes

The problem is the attachment I assigned to the largeTitleTextAttributes attribute attachment is not visible.

如何将附件添加到属性字典中以便附件可见? (我不是在寻找 NSAttributedString初始化(附件:NSTextAttachment)

最佳答案

正如 Apple 的文档所说,您只能指定

You can specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary, using the text attribute keys described in NSAttributedStringKey.

但是你可以直接将UILabel设置为导航栏的标题 View ,比如 使用以下代码

let image1Attachment = NSTextAttachment()
        image1Attachment.image = UIImage(named: "bb")
        image1Attachment.bounds = CGRect.init(x: 0.0, y: 0.0, width: 20, height: 20)
        let image1String = NSAttributedString(attachment: image1Attachment)
        let label: UILabel = UILabel.init(frame: (self.navigationController?.navigationBar.frame)!)
        label.attributedText = image1String
        if #available(iOS 11.0, *) {
            self.navigationItem.titleView = label
            }
        else {
            // Fallback on earlier versions
        }

enter image description here

关于ios - 通过 NSAttributedStringKey 的附件不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46953028/

相关文章:

ios - 下载用于在本地通知上显示的图像不起作用

ios - NSArray 和 NSPredicate 使用 NOT IN

ios - 数据应该驻留在 watchOS 应用程序的什么位置?

swift - 无法将类型 '[AnyObject]' 的值转换为预期参数类型 '[UIUserNotificationAction]?'

ios - UICollectionViewCell 与 UIButton 复选框 - 一次选中一个复选框

android - 有没有适用于 iOS 和 Android 的 Cordova 日历插件?

ios - 对象 C 中的 Unicode 字符

ios - Sprite 套件中的 UIImageView

objective-c - UIImage UIImageJPEGRepresentation 对 UIImage 安全

iphone - 需要 TableViewCell 在被触摸时闪烁