How to line break long large title in iOS 11?
Asked Answered
N

2

6

I am trying to use the new large title system in iOS 11 using Swift. When the title gets too long (see image example), it adds ... instead of line breaking or shrinking the text size. How can I add a line break?

Example image with long title

Here is some of the code I'm using to set up the title:

self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: MyGlobalVariable.themeMainColor]
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 22)]
navigationItem.title = "Search by Name"
Nihi answered 27/9, 2017 at 3:9 Comment(4)
I'm pretty sure that only one line is allowed to be in the title. I've not seen a two line title.Shoal
From everything I've read so far, you may be right. I think I need to approach my problem from a different angle. Thanks!Nihi
Cool, I confirmed it yesterday that only one like is possible in title. Sorry that I didn't update it. Got caught up with work. :)Shoal
Hey no problem. Thanks for the help here.Nihi
N
3

Try this:

for navItem in (self.navigationController?.navigationBar.subviews)! {
    for itemSubView in navItem.subviews {
        if let largeLabel = itemSubView as? UILabel {
            largeLabel.text = self.title
            largeLabel.numberOfLines = 0
            largeLabel.lineBreakMode = .byWordWrapping
        }
    }
}

It worked for me.

enter image description here

Niobous answered 22/1, 2018 at 19:3 Comment(1)
This worked until I had a back button. Now for some reason it doesn't work. When I swipe from the left as if to go back but then let it bounce back into place, the line wrapping starts working. Very strange.Nihi
M
-1

This is Objective-C but what I did worked with backButton. I override the setTitle method of UIViewController like this :

-(void)setTitle:(NSString *)title{

    [super setTitle:title];

    for(id item in self.navigationController.navigationBar.subviews){

        if([item isKindOfClass:[UIView class]]){

            UIView *lc_view = (UIView *)item;

            for(id subItem in lc_view.subviews){

                if([subItem isKindOfClass:[UILabel class]]){

                    UILabel *lc_label = (UILabel *)subItem;

                    lc_label.text = self.title;
                    lc_label.numberOfLines=0;
                    lc_label.lineBreakMode = NSLineBreakByWordWrapping;
                }
            }
        }
    }

    [self.navigationController.navigationBar layoutSubviews];
    [self.navigationController.navigationBar layoutIfNeeded];
}

Then where I want to set the title I simply did :

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    self.title=@"Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";

});

In my case (changing title in UIPageViewController didFinishAnimating method) it seems that a delay < 0.02 didn't work.

Mehalek answered 1/6, 2018 at 15:43 Comment(1)
Sorry, on a real device 0.05 seems to be the right delayMehalek

© 2022 - 2024 — McMap. All rights reserved.