UINavigationItem title label set width?

2019-09-04 17:42发布

In my UIViewController i set my title dynamically so i do not know the length of the string that will be shown in the navigation bar and because of that i have the current situation: alt text http://img94.imageshack.us/img94/2484/picture3vf.png

is there any way to set the width of the label that displays the title in the navigation bar? or should i think of a with, compare the text length to it and if it is too long should i resize it and display the famous "..." ?

I know that i can add an UILabel as subview to the navigationBar but i don't want to use that solution.

Thank you!

3条回答
Evening l夕情丶
2楼-- · 2019-09-04 17:49

I'm not sure how you did that. If I'm creating a navigation view and setting a title that is too long it will be shortened with "..." by default. These black buttons do not seem to be default buttons.

查看更多
地球回转人心会变
3楼-- · 2019-09-04 17:53

You can use sizeWithFont method of NSString to know the size your label will take. Note that you can add a maximum size if you know your label width shouldn't be greater than a certain value

// Get the size really needed for the label string
    CGSize expectedLabelSize = [self.text sizeWithFont:self.font             
    constrainedToSize:maximumSize                                   
    lineBreakMode:self.lineBreakMode];
查看更多
等我变得足够好
4楼-- · 2019-09-04 18:11

Maybe you could add some dummy buttons to the navigation bar that will sit directly underneath your buttons. That would show the normal text behavior, but also allow you to play with your colors.

查看更多
登录 后发表回答