如何从中间截断视图标题

时间:2014-12-24 11:01:27

标签: ios iphone uinavigationitem

我想从中间截断视图标题。

代码: -

self.title = [NSString stringWithFormat:NSLocalizedString(@"SearchResultWithCount", nil),  self.searchString,[self.sortedFileList count]];

输出: - SearchResultWithCount(61 ...

但希望标题类似于: - SearchRe ... thCount(612)。

请帮帮我。

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,请尝试以下代码:

CGSize size=[[UIScreen mainScreen] bounds ].size;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(size.width/2-120, 5, 200, 44)];
titleLabel.text = your_title_text;
titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
[self.navigationItem setTitleView:titleLabel];
[self.navigationController.navigationBar.topItem setTitleView:titleLabel];

答案 1 :(得分:0)

Swift 4

let titleLabel = UILabel()
titleLabel.text = self.searchString
titleLabel.lineBreakMode = .byTruncatingMiddle
navigationItem.titleView = titleLabel