可以通过 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
attributeattachment
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
}
关于ios - 通过 NSAttributedStringKey 的附件不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46953028/