UINavigationBar title label text

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

Is it possible to get the title text to shrink to fit in the UINavigationBar in iOS.

(for portrait iPhone app with no autolayout).

I'm setting the title bar dynamically but sometimes the text is too long and at the moment it just cuts it off with an ellipsis.

i.e. "This is the t..."

I'd like it to shrink the text instead.

回答1:

You can create your own title view to make it.

Something like this:

- (void)viewDidLoad {     [super viewDidLoad];    //Do any additional setup after loading the view, typically from a nib.      UILabel *titleLabelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)]; //<<---- Actually will be auto-resized according to frame of navigation bar;     [titleLabelView setBackgroundColor:[UIColor clearColor]];     [titleLabelView setTextAlignment: NSTextAlignmentCenter];     [titleLabelView setTextColor:[UIColor whiteColor]];     [titleLabelView setFont:[UIFont systemFontOfSize: 27]]; //<<--- Greatest font size     [titleLabelView setAdjustsFontSizeToFitWidth:YES]; //<<---- Allow shrink      // [titleLabelView setAdjustsLetterSpacingToFitWidth:YES];  //<<-- Another option for iOS 6+     titleLabelView.text = @"This is a Title";      navigationBar.topItem.titleView = titleLabelView;      //.... } 

Hope this helps.



回答2:

Xcode 7.1 - Swift 2.0   //Adding Title Label         var navigationTitlelabel = UILabel(frame: CGRectMake(0, 0, 200, 21))         navigationTitlelabel.center = CGPointMake(160, 284)         navigationTitlelabel.textAlignment = NSTextAlignment.Center         navigationTitlelabel.textColor  = UIColor.whiteColor()         navigationTitlelabel.text = "WORK ORDER"         self.navigationController!.navigationBar.topItem!.titleView = navigationTitlelabel 


回答3:

I used this code for swift 4 (note label rect is NOT important..)

func navBar()->UINavigationBar?{         let navBar = self.navigationController?.navigationBar         return navBar     }   override func viewDidLoad() {         super.viewDidLoad()          setTNavBarTitleAsLabel(title: "VERYYYY VERYYYY VERYYYY VERYYYY VERYYYY VERYYYY VERYYYY LONGGGGGG")     }      // will shrink label...      func setTNavBarTitleAsLabel(title: String, color: UIColor ){          // removed some code..          let navigationTitlelabel = UILabel(frame: CGRect(x: 0, y: 0, width: 20, height: 20))         navigationTitlelabel.numberOfLines = 1         navigationTitlelabel.lineBreakMode = .byTruncatingTail          navigationTitlelabel.adjustsFontSizeToFitWidth = true         navigationTitlelabel.minimumScaleFactor = 0.1         navigationTitlelabel.textAlignment = .center         navigationTitlelabel.textColor  = color         navigationTitlelabel.text = title          if let navBar = navBar(){             //was navBar.topItem?.title = title              self.navBar()?.topItem?.titleView = navigationTitlelabel             //navBar.titleTextAttributes = [.foregroundColor : color ?? .black]         }     } 


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