强制执行将所有NavigationItem标题截断为特定数量字符的规则的最佳方法是什么?
到目前为止,我所看到的是以下内容:
继承UINavigationController
并让应用中的所有UINavigationController
继承并使用此方法
@interface MyNavController : UINavigationController {
@end
@implementation MyNavController
- (void)setTitle:(NSString *)title {
// do the truncation here, but it's not working
[super setTitle:myTruncatedTitle];
}
@end
当然,这个navcontroller中的viewcontroller调用self.title = ....
但它没有将这个新的截断标题设置为NavBar中出现的标题。
或者我发现这个解决方案涉及UINavigationItem
上的类别并且调整:
Make all instances of UINavigationBar titles lowercase without subclassing?
您可以建议其他任何解决方案吗?
答案 0 :(得分:1)
self.navigationItem.titleView=[Utility setTitleOfbar:NSLocalizedString(@"title", nil)];
+(UIView *)setTitleOfbar:(NSString *)title{
title = [title substringToIndex: MIN(15, [title length])];
CGSize size=[[UIScreen mainScreen] bounds ].size;
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(size.width/2-120, 5, 200, 44)];
[view setBackgroundColor:[UIColor clearColor]];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0,3,200,44)];
label.text=title;
label.textAlignment=NSTextAlignmentCenter;
label.numberOfLines = 0;
[view addSubview:label];
return view;
}
答案 1 :(得分:0)
在app delegate的didFinishWithLaunching方法中,你可以写下面的代码:
[[UINavigationBar appearance] setTitleTextAttributes:AttributeDictionary];