Is there any way to find the font size and title frame of the UINavigationBar?

Is there a way to programmatically return the font and title bar size of the navigation controller (e.g. self.title.frame.size or self.title.font , although I don't know any of these works)?

(If you're interested, I would like to use the NSString sizeWithFont method to determine how much text can fit in the title before I truncate it. I could do this by physically measuring the font and (for example, I think it's Helvetica 15) but I want the actual title to report its own font and size to make my application look promising.)

+6
source share
2 answers

Starting with iOS 5.0, the UINavigationBar has a titleTextAttributes property. This is a dictionary containing the attributes described in the NSString UIKit Adddition Reference .

If I print this dictionary in one of my projects, I get the following:

 Font = "<UICFFont: 0x998e690> font-family: \".Helvetica NeueUI\"; font-weight: bold; font-style: normal; font-size: 20px"; TextColor = "UIDeviceWhiteColorSpace 0.2 1"; TextShadowColor = "UIDeviceWhiteColorSpace 0 0"; TextShadowOffset = "UIOffset: {0, 0}"; 

... which displays a bold default system font of 20px. You can also change them as you wish.

+3
source

I developed how to get a frame:

self.navigationItem.titleView.frame.size

Which just leaves the font. My research shows that this is a system font of size 20, but I would like it to be self-reporting.

EDIT - Well, that's odd. If I...

  self.title = @"test"; NSLog(@"Width is: %f", self.navigationItem.titleView.frame.size.width); 

... I get random negative numbers from NSLog. Thus, this does not seem to be the correct header.

+1
source

Source: https://habr.com/ru/post/888311/


All Articles