如何动态调整导航栏中的标题大小 [英] How to resize Title in a navigation bar dynamically

查看:132
本文介绍了如何动态调整导航栏中的标题大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些显示在导航控制器中的视图。其中两个视图的导航栏标题较长。

I have some views that show up in a navigation controller. Two of these views have a longer title for the navigation bar.

问题是当标题太长而不适合时,某些字符会被截断并添加...。

The problem is that when the title is too long to fit, some characters are truncated and "..." is added.

有什么方法可以告诉导航栏自动调整标题文本的大小以适应吗?

Is there any way I can tell the Navigation bar to re-size the title text automatically to fit?

推荐答案

在ViewDidload中使用以下代码。

Used the below code in ViewDidload .

目标C

self.title = @"Your TiTle Text";
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];
tlabel.text=self.navigationItem.title;
tlabel.textColor=[UIColor whiteColor];
tlabel.font = [UIFont fontWithName:@"Helvetica-Bold" size: 30.0];
tlabel.backgroundColor =[UIColor clearColor];
tlabel.adjustsFontSizeToFitWidth=YES;
tlabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView=tlabel;

Swift版本

self.title = "Your TiTle Text"
let tlabel = UILabel(frame: CGRectMake(0, 0, 200, 40))
tlabel.text = self.title
tlabel.textColor = UIColor.whiteColor()
tlabel.font = UIFont(name: "Helvetica-Bold", size: 30.0)
tlabel.backgroundColor = UIColor.clearColor()
tlabel.adjustsFontSizeToFitWidth = true
tlabel.textAlignment = .center;
self.navigationItem.titleView = tlabel

希望它适合你。谢谢

这篇关于如何动态调整导航栏中的标题大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆