Verma
Verma

Reputation: 257

How to truncate the view title from middle

I want to truncate the view title from middle.

Code:-

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

Output:- SearchResultWithCount(61...

but want to want the title something like :- SearchRe...thCount(612).

Please help me out.

Upvotes: 2

Views: 1659

Answers (2)

Knut Valen
Knut Valen

Reputation: 256

Swift 4

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

Upvotes: 0

md. ariful ahsan
md. ariful ahsan

Reputation: 606

I was having the same problem, try the following code:

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];

Upvotes: 1

Related Questions