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

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

问题描述

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

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;

斯威夫特版

self.title = "Your Title Text"
let tlabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
tlabel.text = self.title
tlabel.textColor = UIColor.white
tlabel.font = UIFont.systemFont(ofSize: 30, weight: .bold)
tlabel.backgroundColor = UIColor.clear
tlabel.adjustsFontSizeToFitWidth = true
tlabel.textAlignment = .center
self.navigationItem.titleView = tlabel

希望它对你有用.谢谢

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

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