如何在UINavigationBar中设置自定义字体?

Vol*_*da2 6 iphone uinavigationbar

如何在UINavigationBar中设置自定义字体?我需要tahoma字体.

- (void)viewDidLoad{
    self.title =@"My text";
}
Run Code Online (Sandbox Code Playgroud)

替代文字

lxt*_*lxt 21

完全可能,如果有点棘手的话.一旦你找到了你需要的字体(iOS中附带的替代品之一,或者你已获得正确许可的TTF文件),只需创建一个包含大小,格式,字体等的UILabel然后添加它到该栏的导航项目(或者,如果您在视图控制器中执行此操作,请将该控制器的.navigationItem.titleView设置为您的标签).

例如,我在UINavigationController中有一个视图控制器.要将顶部的标签更改为自定义字体,我只需执行以下操作:

//...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];
Run Code Online (Sandbox Code Playgroud)


Cyn*_*era 17

这段代码应该有效.在uiviewcontroller中展示你的主要用户名:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

请注意,生成的自定义视图具有透明背景,因此您可以使用[navigationController.navigationBar addSubview:view]向导航栏添加更多内容.这可能是酒吧左角的微调器或其他东西.

如果您使用自定义视图,则无法再使用uiviewcontroller标题设置标题.您需要使用自定义视图提供的方式.

例:

((UILabel *)self.navigationItem.titleView).text = title;
Run Code Online (Sandbox Code Playgroud)