Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set custom font in UINavigationBar?

How can I set custom font in UINavigationBar ? I need the tahoma font.

- (void)viewDidLoad{
    self.title =@"My text";
}

alt text

like image 950
Voloda2 Avatar asked Jan 15 '11 13:01

Voloda2


People also ask

How do I change the font style of the navigation bar title in Swift?

let navigation = UINavigationBar. appearance() let navigationFont = UIFont(name: "Custom_Font_Name", size: 20) let navigationLargeFont = UIFont(name: "Custom_Font_Name", size: 34) //34 is Large Title size by default navigation. titleTextAttributes = [NSAttributedStringKey. foregroundColor: UIColor.

How do I change the navigation bar on my Iphone?

Change the Bar Style A user changes the navigation bar's style, or UIBarStyle , by tapping the “Style” button to the left of the main page. This button opens an action sheet where users can change the background's appearance to default, black-opaque, or black- translucent.

How do I customize my navigation bar in SwiftUI?

titleview in UIKit. To customize a navigation bar title view in SwiftUI, we simply set ToolbarItem of placement type . principal to a new . toolbar modifier.


2 Answers

Totally possible, if a little tricky to do. Once you've found the font you need (either one of the alternatives already shipped with iOS, or a TTF file that you've got the correct licensing for), just create a UILabel with the size, formatting, font etc and then add it to the navigation items for that bar (or, if you're doing this in a view controller, set the .navigationItem.titleView of that controller to your label).

For example, I have a view controller inside a UINavigationController. To change the label in the top to a custom font, I simply do:

//...I have already setup a UILabel called navLabel that has the same style as a 
// default navigation bar title, but I've changed the .font property to my custom font
self.navigationItem.titleView = navLabel;
[navLabel release];
like image 195
lxt Avatar answered Oct 02 '22 02:10

lxt


This code should work. In uiviewcontroller which presents your main ui:

- (void)viewDidLoad
{
        [super viewDidLoad];

        int height = navigationController.navigationBar.frame.size.height;
        int width = navigationController.navigationBar.frame.size.width;

        UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        navLabel.backgroundColor = [UIColor clearColor];
        navLabel.textColor = [UIColor whiteColor];
        navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        navLabel.font = [UIFont boldSystemFontOfSize:15];
        navLabel.textAlignment = UITextAlignmentCenter;
        self.navigationItem.titleView = navLabel;
        [navLabel release];
}

Note that resulting custom view has transparent background, so that you can add something more to your navigation bar with [navigationController.navigationBar addSubview:view]. This may be spinner in left corner of the bar or something else.

If you use custom view, you will not be able set the title with uiviewcontroller title anymore. You need to use the way available by your custom view.

Example:

((UILabel *)self.navigationItem.titleView).text = title;
like image 45
Cynichniy Bandera Avatar answered Oct 02 '22 01:10

Cynichniy Bandera