简体   繁体   English

使用TitleView在IOS的导航栏上添加两行文本

[英]Add 2 lines of text on Navigation Bar in IOS using TitleView

I have to add two lines of text with different font sizes on Navigation Bar using Title View. 我必须使用标题视图在导航栏上添加两行具有不同字体大小的文本。

I have tried the following code: 我尝试了以下代码:

I want EREDITOMETRO as bold with large font and LA SUCCESSIONE A PORTATA DI MANO with small font 我希望EREDITOMETRO为大字体加粗字体,而LA SUCCESSIONE为小字体的PORTATA DI MANO

UILabel * label =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 480, 44)]; label.backgroundColor =[UIColor clearColor]; 
label.numberOfLines =2; 
label.font =[UIFont boldSystemFontOfSize:9.0f];
label.textColor =[UIColor whiteColor]; 
label.text = @"EREDITOMETRO \n LA SUCCESSIONE A PORTATA DI MANO"; 

self.navigationItem.titleView = label;

Try This - 尝试这个 -

NSString *stringName = @"Welcome Home" ;
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:stringName];
[attrString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize: 14.0f] range:NSMakeRange(0, 4)];

UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 480, 44)];
label.numberOfLines =0;
label.attributedText = attrString;
self.navigationItem.titleView = label;
- (void)setTitle:(NSString *)title
{
    [super setTitle:title];
    UILabel *titleView = (UILabel *)self.navigationItem.titleView;
    if (!titleView) {
        titleView = [[UILabel alloc] initWithFrame:CGRectZero];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.font = [UIFont boldSystemFontOfSize:20.0];
        titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        titleView.numberOfLines = 2;
        titleView.textColor = [UIColor yellowColor]; 
        self.navigationItem.titleView = titleView;
       } 
    }

you need to make a custom label and pass it to navigation item like this 您需要制作一个自定义标签,并将其传递给这样的导航项

    let label: UILabel = UILabel(frame: CGRectMake(0, 0, 300, 50))
    label.backgroundColor = UIColor.clearColor()
    label.numberOfLines = 2
    label.font = UIFont.boldSystemFontOfSize(14.0)
    label.textAlignment = .Center
    label.textColor = UIColor.whiteColor()
    label.text = "Your Text"
    self.navigationItem.titleView = label

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM