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

时间:2015-04-06 10:45:31

标签: ios uinavigationbar titleview

我必须使用标题视图在导航栏上添加两行不同字体的文本。

我尝试过以下代码:

我希望EREDITOMETRO为大字体粗体和LA SUCCESSIONE A 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;

2 个答案:

答案 0 :(得分:2)

试试这个 -

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;

答案 1 :(得分:0)

- (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;
       } 
    }