Showing posts with label nstextattachment. Show all posts
Showing posts with label nstextattachment. Show all posts

Saturday, November 4, 2017

Attachment via NSAttributedStringKey is not visible

Leave a Comment

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 largeTitleTextAttributes attribute attachment is 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         } 

enter image description here

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.

Read More

Wednesday, March 30, 2016

Update image of NSTextAttachment once already rendered

Leave a Comment

I have an NSTextAttachment which I want to show a loading image until the image has downloaded and then once it has I want to update the image.

I have all of the logic in place, except when I call textAttachment.image = image the second time nothing happens.

How can I update the NSTextAttachment once it has already been rendered by the UITextView?

Thanks!

1 Answers

Answers 1

You must always update the user interface from the main thread, not from the socket thread.

Here is a Swift example:

// Update UI from main thread dispatch_async(dispatch_get_main_queue(), {   textAttachment.image = image })
Read More