iOS多行标题栏

时间:2014-10-13 16:06:19

标签: ios objective-c uinavigationcontroller

我已经尝试了这里发布的关于此的所有答案,并且我已经实现了多行但是我需要从上面开始从现在开始的一些文本。 请看屏幕截图,它显示我想要标题标签的起始位置。
http://i59.tinypic.com/23jl4jk.png

这是我的代码 -

UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, 44)];
[titleLabel setNumberOfLines:3];
[titleLabel setBackgroundColor:[UIColor darkGrayColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:30.0]];
[titleLabel setText:@"LET'S GET YOU REGISTERED"];

self.navigationItem.titleView = titleLabel;

我尝试用UIView替换标签,但仍然没有运气。
我的自定义导航控制器文件中的代码是 -

@interface UINavigationBar (myNave)
- (CGSize)changeHeight:(CGSize)size;
@end

@implementation UINavigationBar (customNav)
- (CGSize)sizeThatFits:(CGSize)size {
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height * 0.15;
    CGSize newSize = CGSizeMake(screenWidth,screenHeight);
    return newSize;
}
@end

请帮忙。

1 个答案:

答案 0 :(得分:3)

您应该将numberOfLines设置为适合文本长度。您可以通过将行数设置为零来完成此操作。此外,30号字体不允许多行文本适合UINavigationItem(没有sizeToFit)。您可以考虑将UINavigationBar设置为清晰,并创建自己的titleView以适应屏幕顶部(如果您需要它为30)。

然而,类似于此的内容将为您提供完全适合导航的多行文字:

UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(50, 0, self.navigationController.navigationBar.frame.size.width-100, self.navigationController.navigationBar.frame.size.height)];

UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, titleView.frame.size.width, titleView.frame.size.height)];
[titleLabel setTextAlignment:NSTextAlignmentCenter];
[titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:12.0]];
[titleLabel setNumberOfLines:0];

NSString *titleString = @"This is a\n multiline string";
[titleLabel setText:titleString];

[titleView addSubview:titleLabel];

[self.navigationController.navigationItem setTitleView:titleView];