【问题标题】:best way to truncate all UINavigationBar titles in the app截断应用程序中所有 UINavigationBar 标题的最佳方法
【发布时间】:2014-01-15 04:07:30
【问题描述】:

执行将所有 NavigationItem 标题截断为特定字符数的规则的最佳方法是什么?

到目前为止,我看到的情况如下:

继承UINavigationController 并让应用程序中的所有UINavigationControllers 继承并使用此方法

@interface MyNavController : UINavigationController {

@end

@implementation MyNavController 

- (void)setTitle:(NSString *)title {

    // do the truncation here, but it's not working
    [super setTitle:myTruncatedTitle];
}

@end

当然,只要此导航控制器中的视图控制器调用 self.title = ...,它就会进入此方法。

但它没有将这个新的截断标题设置为出现在导航栏中的标题。

或者我找到了这个解决方案,它涉及UINavigationItem 上的一个类别和 swizzling:

Make all instances of UINavigationBar titles lowercase without subclassing?

您还有其他可以建议的解决方案吗?

【问题讨论】:

  • 您的所有 ViewController 是否都使用 MyNavController 导航类推送?此外,您的继承代码中有一个拼写错误: UINavigation 应该是 UINavigation
  • 谢谢,没有从我的实际代码中复制。为了回答你的问题,是的,他们被推到了这个导航控制器中。

标签: ios iphone objective-c cocoa-touch uinavigationcontroller


【解决方案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;
    }

【讨论】:

    【解决方案2】:

    在应用代理的didFinishWithLaunching方法中,您可以编写如下代码:

    [[UINavigationBar appearance] setTitleTextAttributes:AttributeDictionary];
    

    【讨论】:

    • 不信有text属性可以设置标题的长度吗?
    猜你喜欢
    • 1970-01-01
    • 2021-09-24
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多