iphone - UINavigationBar 标题标签文字

标签 iphone ios objective-c uinavigationbar

是否可以让标题文本缩小以适应 iOS 中的 UINavigationBar

(适用于没有自动布局的纵向 iPhone 应用)。

我正在动态设置标题栏,但有时文本太长,此时它只是用省略号将其截断。

即“这是……”

我希望它能缩小文本。

最佳答案

您可以创建自己的标题 View 来制作它。

是这样的:

- (void)viewDidLoad
{
    [super viewDidLoad];

  //Do any additional setup after loading the view, typically from a nib.
     UILabel *titleLabelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)]; //<<---- Actually will be auto-resized according to frame of navigation bar;
    [titleLabelView setBackgroundColor:[UIColor clearColor]];
    [titleLabelView setTextAlignment: NSTextAlignmentCenter];
    [titleLabelView setTextColor:[UIColor whiteColor]];
    [titleLabelView setFont:[UIFont systemFontOfSize: 27]]; //<<--- Greatest font size
    [titleLabelView setAdjustsFontSizeToFitWidth:YES]; //<<---- Allow shrink
     // [titleLabelView setAdjustsLetterSpacingToFitWidth:YES];  //<<-- Another option for iOS 6+
    titleLabelView.text = @"This is a Title";

    navigationBar.topItem.titleView = titleLabelView;

    //....
}

希望这对您有所帮助。

关于iphone - UINavigationBar 标题标签文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14505331/

相关文章:

ios - 一个有争议的类似委托(delegate)的事件通知模式

iphone - 在 subview 中添加/删除 View 时对 View 进行动画处理

ios - UIFontPickerViewController 在 Catalyst 中工作吗?

c++ - 作为可选函数指针的类参数不被识别为函数

ios - 如何将 UILabel 添加到 UIScrollView?

iphone - 2D 平台游戏的最佳 iPhone 框架

ios - 使用 UISegmentControl 切换 View 时 UISearchBar 消失

iOS:UISearchBar 中的 UIActivityIndi​​cator

objective-c - 类似于 Xcode 4 中的渐变按钮栏

ios - 如何制作精确的模糊