0

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

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;
Wain
  • 118,658
  • 15
  • 128
  • 151
Anagha Magar
  • 21
  • 1
  • 6

3 Answers3

2

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;
Uttam Sinha
  • 722
  • 6
  • 9
  • as per your example of Welcome home ,I want Welcome on 1st line with bold and on second line Home with another font size. – Anagha Magar Apr 06 '15 at 12:15
  • Welcome :) For different font you can use NSMutableAttributedString and addAttribute method help you achieving what you are looking for. – Uttam Sinha Apr 06 '15 at 12:43
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;
       } 
    }
CallMeSan
  • 66
  • 3
0

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
Ahmad Taalab
  • 205
  • 4
  • 16