带有UnderLine Text的UINavigationBar setTitleTextAttributes

har*_*ios 3 xcode fonts objective-c ios

我正在尝试setTextAttribute使用此代码在所有视图中使用UnderLine

 [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],
//        NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
        (NSString *) kCTUnderlineStyleAttributeName:@(kCTUnderlineStyleDouble),
        NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Bold" size:17]}];
Run Code Online (Sandbox Code Playgroud)

但它不工作,我知道通过创建自定义的另一种方法UILabelNSAttributedString和设置它在titleview的,但没有任何其他的方式来实现这一目标使用外观协议?

Vij*_*com 7

NSUnderlineStyleAttributeName应该为您完成这项工作.

试试这个Dude:

    NSDictionary *attributes = @{
                                 NSUnderlineStyleAttributeName: @1,
                                 NSForegroundColorAttributeName : [UIColor redColor],
                                 NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Bold" size:17]
                                 };

    [[UINavigationBar appearance] setTitleTextAttributes:attributes];
Run Code Online (Sandbox Code Playgroud)

更新:

我提到Apple的文档,似乎不可能通过外观协议.

它说

"您可以使用NSString UIKit Additions Reference中描述的文本属性键为文本属性字典中的标题指定字体,文本颜色,文本阴影颜色和文本阴影偏移."

我也试过创建新的简单项目.看不到这条线.

在此输入图像描述 在此输入图像描述

Apple文件:

UINavigationBar的

用于文本属性字典的NSString键