Check if UINavigationItem title is truncated
- by PartiallyFinite
I want to check whether the title of a UINavigationItem is truncated.
I set the title like this: self.navigationItem.title = whatever.
I know I can check if the text in a UILabel is truncated like this:
CGSize size = [label.text sizeWithFont:[UIFont fontWithName:@"myfont" size:18.0]];
if (size.width > label.bounds.size.width) {
// set a shorter title
}
And I can even find the UINavigationItemView object in which the title is displayed like so:
UIView *navItemView;
for (UIView *view in self.navigationController.navigationBar.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UINavigationItemView")]) {
navItemView = view;
}
}
But I cannot apply this method to the navItemView because is always seems to have a width of exactly 58, which is much less than the title in it, so according to that, it would appear that the title is truncated, even when it isn't.
So, my question comes down to this: How do I find the width of the title displayed in the UINavigationItem?
UPDATE: I have found a solution to my problem, but it isn't exactly ideal, perfect, or reliable, so I am not marking it as an answer yet. If anyone has any better solutions, please share them.