ios – 带有UnderLine Text的UINavigationBar setTitleTextAttributes

前端之家收集整理的这篇文章主要介绍了ios – 带有UnderLine Text的UINavigationBar setTitleTextAttributes前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用此代码在所有视图中使用UnderLine setTextAttribute

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

但它不起作用,我知道另一种方法,使用NSAttributedString创建自定义UILabel并在TitleView上设置它,但有没有其他方法来实现这个使用外观协议?

解决方法

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

试试这个Dude:

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

    [[UINavigationBar appearance] setTitleTextAttributes:attributes];

更新:

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

它说

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

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

Apple文件

UINavigationBar

NSString Keys for Text Attributes Dictionaries

猜你在找的Xcode相关文章