0

Under iOS 7 I'd like to replace the title text view in a navigation bar by a custom view that includes another text view that looks identical to the default one. (Essentially, I need to add another view to the title element, but to do that I have to replace the whole thing).

How can I create a UILabel that's guaranteed to look the same as the default (same font, size, etc?)

In other words - how can I programmatically find the font face and size used by the OS for the title view of a navigation bar.

tarmes
  • 15,366
  • 10
  • 53
  • 87

1 Answers1

0
UILabel *titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(30, 0, 100, 40)];
[titleLabel setBackgroundColor:[UIColor redColor]];
[titleLabel setFont:[UIFont systemFontOfSize:14]];
[titleLabel setTextColor:[UIColor blackColor]];
[titleLabel setText:@"Rajneesh071"];
[self.navigationItem.titleView addSubview:titleLabel];

If you want to use your own font then you can use this.

[UIFont fontWithName:@"FontName" size:14]

You can get font name using this.This will print HelveticaNeueInterface-M3

NSLog(@"%@", [UIFont systemFontOfSize:14].fontName);

And size

NSLog(@"%f",[UIFont systemFontSize]);

Follow this answer How to use custom fonts in iPhone SDK?

Community
  • 1
  • 1
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
  • Without setting a font, that display's nothing at all. The crux of my problem is to work out how to use the same font as iOS would have done. I'm currently doing this: titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]. That seems to work, but there's no guaranty – tarmes Oct 15 '13 at 08:45
  • systemFontOfSize: this will solve your problem, check edited answer – Rajneesh071 Oct 15 '13 at 08:57
  • That won't guarantee it. What happens if apple change the size slightly in an update... – tarmes Oct 15 '13 at 08:59
  • how can he update size, you are specifying size.. check ans again – Rajneesh071 Oct 15 '13 at 09:13
  • I want the size to be the same the the OS would have used had I not replaced the view. Fixing the size to 14 point may be the same as iOS 7.02 would have used, but it may not be the same as 7.03 uses... – tarmes Oct 15 '13 at 09:59
  • NSLog(@"%f",[UIFont systemFontSize]); – Rajneesh071 Oct 15 '13 at 10:51
  • That's the system font size. It isn't necessarily the size that the OS will use for the title text! – tarmes Oct 15 '13 at 11:15