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

发布于 2024-10-12 07:36:50 字数 191 浏览 2 评论 0原文

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

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

替代文字

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

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

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在巴黎塔顶看东京樱花 2024-10-19 07:36:51

这段代码应该可以工作。在呈现主 ui 的 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];
}

请注意,生成的自定义视图具有透明背景,以便您可以使用 [navigationController.navigationBar addSubview:view] 在导航栏中添加更多内容。这可能是位于栏左角的旋转器或其他东西。

如果您使用自定义视图,您将无法再使用 uiviewcontroller title 设置标题。您需要使用自定义视图可用的方式。

例子:

((UILabel *)self.navigationItem.titleView).text = title;

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;
眸中客 2024-10-19 07:36:50

完全有可能,只是做起来有点棘手。一旦你找到了你需要的字体(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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文