The new large title feature can be customised via largeTitleTextAttributes which is (like any other attributes) a dictionary with NSAttributedStringKey keys. One of these keys is NSAttachmentAttributeName/attachment.
Consider this:
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
largeTitleTextAttributesattributeattachmentis not visible.
How to add an attachment into an attributes dictionary so the attachment will be visible? (I'm not looking for the NSAttributedString's init(attachment: NSTextAttachment)
2 Answers
Answers 1
AS Apple's Doc said you can only specifiy
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.
But you can directly set UILabel to title view of navigation bar like using following code
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 } Answers 2
From looking at Apple's Docs, the list of attributes you can specify in titleTextAttributess seems limited:
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.
https://developer.apple.com/documentation/uikit/uinavigationbar/1624953-titletextattributes
Sadly, image attachments isn't on the list.

0 comments:
Post a Comment