使用自定义字体时,UINavigationBar标题会被截断 [英] UINavigationBar title gets truncated when using custom font

查看:117
本文介绍了使用自定义字体时,UINavigationBar标题会被截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码自定义UINavigationBar标题外观后,带有文本的标签会被截断,如下图所示:

after using this code to custom the UINavigationBar title appearance, the label with text gets truncated, as the image below shows:

[[UINavigationBar appearance] setTitleTextAttributes:@{
                            UITextAttributeTextColor : [UIColor whiteColor],
                            UITextAttributeFont : [UIFont fontWithName:@"Intro" size:20.0f],
                            UITextAttributeTextShadowColor : [UIColor clearColor]
                            }];

而且,正如您所看到的,还有足够的空间。

And, as you can see, there is enough space.

任何想法?

推荐答案

iOS 9更新



我'我已经在一个干净的项目中进行了一些测试,其中包含几十种不同大小的内置字体,我想我可以肯定地说,早期版本的iOS中发现的标签尺寸问题已经修复(或之前) )iOS 9。

Update for iOS 9

I've done a fair bit of testing in a clean project with a few dozen built-in fonts at various sizes, and I think I can state with some confidence that the label sizing issues found in earlier versions of iOS have been fixed in (or before) iOS 9.

原始问题中描述的用例似乎不具有可再现性,标题标签现在似乎可以自行调整大小。因此,我认为不再需要手动更新布局。

The use case decribed in the original question does not seem to be reproducible, and the title label now seems to resize properly on its own. As such, I don't think it's necessary to update the layout manually anymore.

如果在显然有足够的可视空间时你仍然看到截断问题导航栏,您可以尝试以下几种方法:

If you are still seeing truncation issues when there is clearly plenty of visual space available in the navigation bar, there are a few things you could try:


  1. 删除您可能用于解决问题的所有额外视图。例如,如果您要创建自己的 UILabel 并将其设置为导航栏的 titleView ,则可以停止这样做,并且只是正常设置标题。

  2. 删除尽可能多的代码来调整导航栏的大小和 titleView 。这包括在下面最初接受的答案中找到的代码。

  3. 如果您使用的是自定义字体(即iOS中未包含的字体),请对其进行验证以确保它没有损坏,并包含iOS正确测量所需的所有元数据。如果字体损坏,使用时可能显示不正确。

  1. Remove any extra views that you may have been using to workaround the issue. For example, if you are creating your own UILabel and setting it as the navigation bar's titleView, you can stop doing that, and just set the title normally.
  2. Remove as much code as possible that adjusts the sizing of the navigation bar and titleView. That includes the code found in the originally accepted answer below.
  3. If you are using a custom font (i.e., one not included with iOS), validate it to make sure that it is not damaged, and contains all of the metadata necessary for iOS to measure it properly. If a font is damaged, it can appear incorrectly when used.






原始答案

UINavigationBar布局存在一些已知问题。尝试在视图控制器出现时和/或轮换时更新布局。

There are some known issues with UINavigationBar layout. Try updating the layout when the view controller appears, and/or on rotation.

- (void)viewDidLoad
{
    [super viewDidLoad];
    ...
    [[[self navigationController] navigationBar] setNeedsLayout];
}

这篇关于使用自定义字体时,UINavigationBar标题会被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆