UINavigationItem titleView position

前端 未结 7 911
长发绾君心
长发绾君心 2021-02-13 03:45

I\'m using UINavigationItem\'s titleView property to set a custom UILabel with my desired font size/color. Here\'s my code:

self.headerLabel = [[UILabel alloc] i         


        
7条回答
  •  没有蜡笔的小新
    2021-02-13 04:04

    Just calculate exact frame size needed and align to left:

    UIFont* font = [UIFont fontWithName:@"Bitsumishi" size:20];
    CGSize maximumLabelSize = CGSizeMake(296,9999);
    CGSize expectedLabelSize = [title sizeWithFont:font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeCharacterWrap]; 
    CGRect frame = CGRectMake(0, 0, expectedLabelSize.width, expectedLabelSize.height);
    UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
    label.font = font;
    label.textAlignment = UITextAlignmentLeft;
    label.text = title;
    self.titleView = label;
    

提交回复
热议问题