为导航标题文本设置截断模式

时间:2010-11-29 23:04:07

标签: iphone uinavigationitem

我想为UINavigationItem中显示的文本配置截断模式。

我遇到了几种不同的配置字体大小的解决方案,但不知道如何使用NSString上存在的UILineBreakModeHeadTruncation。

我如何配置字体大小:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
[label setFont:[UIFont boldSystemFontOfSize:16.0]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:self.title];
[self.navigationController.navigationBar.topItem setTitleView:label];
[label release];

(基于recommendation from here

如果我在loadView或viewDidLoad中调用它,它似乎工作但在该方法之外,在加载视图后,它似乎不起作用。不知道为什么会这样。

1 个答案:

答案 0 :(得分:2)

你尝试了什么,什么不起作用?你应该能够做到:

label.lineBreakMode = UILineBreakModeHeadTruncation;

或者换行到多行:

label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;