导航栏设置带有下划线文本的标题文本属性

3

我正在尝试使用以下代码在所有视图中设置带有下划线的文本属性:

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

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

1个回答

7

NSUnderlineStyleAttributeName可以帮你实现这个功能。

试试这个,伙计:

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

    [[UINavigationBar appearance] setTitleTextAttributes:attributes];

更新:

我参考了Apple的文档,似乎通过外观协议不可能实现。

文档中写到:

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

我尝试创建了一个新的简单项目,但是没有看到这行代码。

enter image description here enter image description here

Apple文档:

UINavigationBar

NSString Keys for Text Attributes Dictionaries


我已经尝试了Dude,但在导航栏上显示红色标题时无法工作。 - hardikdevios
你把这段代码放在哪里了?是放在AppDelegate的didFinishLaunching方法里吗? - Vijay-Apple-Dev.blogspot.com
在AppDelegate上是不是需要在Storyboard或Plist中进行特殊设置才能影响它? - hardikdevios
创建一个新的示例项目,然后在其中放置导航栏,使用此代码进行测试。这可能会帮助您找出问题是在此代码中还是在您更改属性的某个地方! - Vijay-Apple-Dev.blogspot.com
嗯...在这段代码或我尝试的任何代码中,下划线都无法正常工作...你在@Vijay-Apple-Dev.blogspot.com上看到了吗? - Lyndsey Scott
@vijay 这对我也不起作用,在任何其他代码中都是如此,我正在使用iOS 8 SDK iPhone 6模拟器。 - hardikdevios

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接