iphone – 如何在UINavigationController中添加多行标题栏

iphone – 如何在UINavigationController中添加多行标题栏,第1张

概述我试着在UINavigationController中添加一个两行标题栏 我想调整字体大小根据字符串长度自动设置.我的字符串最大大小为60.我尝试通过以下代码实现 UILabel *bigLabel = [[UILabel alloc] init];bigLabel.text = @"1234567890 1234567890 1234567890 1234567890 1234567890 1 我试着在UINavigationController中添加一个两行标题栏

我想调整字体大小根据字符串长度自动设置.我的字符串最大大小为60.我尝试通过以下代码实现

UILabel *bigLabel = [[UILabel alloc] init];bigLabel.text = @"1234567890 1234567890 1234567890 1234567890 1234567890 123456";bigLabel.backgroundcolor = [UIcolor clearcolor];bigLabel.textcolor = [UIcolor whitecolor];bigLabel.Font = [UIFont boldSystemFontOfSize:20];bigLabel.adjustsFontSizetoFitWIDth = YES;bigLabel.clipsToBounds = NO;bigLabel.numberOflines = 2;bigLabel.textAlignment = ([self.Title length] < 10 ? NSTextAlignmentCenter : NSTextAlignmentleft);[bigLabel sizetoFit];self.navigationItem.TitleVIEw = bigLabel;

这对我没用,能帮助我吗?我必须为iPhone和iPad屏幕做这个

解决方法 只需设置UILabel的setNumberOflines:0即可.请参阅下面的示例.

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0,0.0,320.0,44.0)];[label setBackgroundcolor:[UIcolor clearcolor]];[label setNumberOflines:0];[label setTextcolor:[UIcolor whitecolor]];[label setTextAlignment:NSTextAlignmentCenter];[label setText:@"1234567890 1234567890 1234567890 1234567890 1234567890 123456"];self.navigationItem.TitleVIEw = label;

设置左和右UIbarbuttonItems – 您可以添加它们.

UIbarbuttonItem *leftbarbutton = [[UIbarbuttonItem alloc]initWithTitle:@"left" style:UIbarbuttonItemStylePlain target:self action:@selector(leftPress:)];self.navigationItem.leftbarbuttonItem = leftbarbutton;UIbarbuttonItem *rightbarbutton = [[UIbarbuttonItem alloc]initWithTitle:@"Right" style:UIbarbuttonItemStylePlain target:self action:@selector(rightPress:)];self.navigationItem.rightbarbuttonItem = rightbarbutton;

还要减少Label的FontSize.在这种情况下它是12.
它看起来像这样:

对于扩展的问题:

只需在以前的代码中进行一些更改 –

[label setNumberOflines:2]; //Set it 2 instead of 0Nsstring *TitleStr = @"3456456676456464554541223434484384233456744444444456785643367";//Check your string length then set Font according to it.//I have set Font size according to your requirement//which is 0-60.if([TitleStr length]>40 && [TitleStr length]<=52){    [label setFont:[UIFont FontWithname:@"Arial" size:13.0]];}else if([TitleStr length]>52){    [label setFont:[UIFont FontWithname:@"Arial" size:11.5]];}[label setText:TitleStr];

注意:您不能使用UILabel的adjustsFontSizetoFitWIDth:属性,因为它不适用于setNumberOflines:0在您的情况下,您必须使用if条件处理它.

这是根据UILabel的宽度设置FontSize的方法.

[label setNumberOflines:1];label.adjustsFontSizetoFitWIDth = YES;label.minimumFontSize = 1.0;
总结

以上是内存溢出为你收集整理的iphone – 如何在UINavigationController中添加多行标题栏全部内容,希望文章能够帮你解决iphone – 如何在UINavigationController中添加多行标题栏所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1078999.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存