How to line break long large title in iOS 11?

﹥>﹥吖頭↗ 提交于 2019-11-30 09:56:37

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!