UINavigationItem title label set width?

2019-09-04 17:33发布

问题:

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!

回答1:

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.



回答2:

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:

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