2

新的大标题功能可以通过largeTitleTextAttributes它(与任何其他属性一样)带键进行自定义。这些键之一是/ 。dictionaryNSAttributedStringKeyNSAttachmentAttributeNameattachment

考虑一下:

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

问题是我分配给largeTitleTextAttributes属性的附件attachment 不可见

如何将附件添加到属性字典中以使附件可见? (我不是在寻找NSAttributedString's init(attachment: NSTextAttachment

4

2 回答 2

1

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

您可以使用 NSAttributedStringKey 中描述的文本属性键为文本属性字典中的标题指定字体、文本颜色、文本阴影颜色和文本阴影偏移量。

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

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
        }

在此处输入图像描述

于 2017-11-04T06:07:57.337 回答
0

通过查看 Apple 的文档,您可以在 titleTextAttributess 中指定的属性列表似乎有限:

您可以使用 NSAttributedStringKey 中描述的文本属性键为文本属性字典中的标题指定字体、文本颜色、文本阴影颜色和文本阴影偏移量。

https://developer.apple.com/documentation/uikit/uinavigationbar/1624953-titletextattributes

可悲的是,图像附件不在列表中。

于 2017-10-29T08:26:04.817 回答