Anagha Magar
Anagha Magar

Reputation: 21

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

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;

Upvotes: 0

Views: 3877

Answers (3)

Ahmad Taalab
Ahmad Taalab

Reputation: 215

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

Upvotes: 0

Uttam Sinha
Uttam Sinha

Reputation: 722

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;

Upvotes: 2

CallMeSan
CallMeSan

Reputation: 66

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

Upvotes: 0

Related Questions