iphone - 如何动态调整导航栏中的标题大小

标签 iphone uinavigationcontroller resize uinavigationbar autoresize

我有一些 View 显示在导航 Controller 中。其中两个 View 的导航栏标题较长。

问题在于,当标题太长而无法容纳时,某些字符会被截断并添加“...”。

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

最佳答案

在 ViewDidload 中使用以下代码。

objective-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: 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

希望它对你有用。谢谢

关于iphone - 如何动态调整导航栏中的标题大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14956561/

相关文章:

iphone - 如何解析 iPhone 应用程序中的 HTML 子标签?

iphone - 改变 UIImageView bounds.size.height 的 iOS 行为

ios - 为 UITableView 标题创建数组

ios - 从大标题页转换到小标题页时出现导航间隙

css - 防止背景图像缩放

iphone - 在特定时间执行某项功能

iphone - 如何替换NavigationController Stack中的viewController?

iphone - 单击 "back"按钮时 UINavigationController 应用程序崩溃

flutter - 无法在 flutter 中更改按钮的高度

c++ - Qt:设置后如何清除setFixedSize