UINavigationItem title label set width?

别说谁变了你拦得住时间么 提交于 2019-12-25 01:46:09

问题


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


来源:https://stackoverflow.com/questions/2281125/uinavigationitem-title-label-set-width

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