是否有一种方法可以在不改变字体的情况下将导航栏中的标题更改为斜体,粗体和下划线?



我使用UIAppearance来改变导航栏标题的属性,如下所示:

[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [MM mainTitleColor]}];

但是我没有找到使文本加下划线或斜体的方法,有没有一种方法可以在不改变字体的情况下做到这一点?

No。这些属性你不能改变,除非字体改变。因为外观代理下的可用键是

  • UITextAttributeFont
  • UITextAttributeTextColor
  • UITextAttributeTextShadowColor
  • UITextAttributeTextShadowOffset

更改这些属性以自定义UINavigationBar

如果你想改变字体,请看下面的例子

[[UINavigationBar appearance] setTitleTextAttributes:@{
    UITextAttributeTextColor: TEXT_COLOR,
    UITextAttributeTextShadowColor: SHADOW_COLOR,
    UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
    UITextAttributeFont: [UIFont fontWithName:@"Arial-Bold" size:0.0],
}];

试试这个

你必须添加一个imageview来定制你的导航控制器。

UIFont *yourFont = [UIFont fontWithName:@"Helvetica-BoldOblique" size:[UIFont systemFontSize]];
if ([[UINavigationBar class]respondsToSelector:@selector(appearance)]) {
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"myimage.png"] forBarMetrics:UIBarMetricsDefault];
}
[[UINavigationBar appearance] setTitleTextAttributes:@{
UITextAttributeTextColor : [UIColor clearColor],
UITextAttributeTextShadowColor : [UIColor blackColor],
UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(1, 0)],
UITextAttributeFont : yourFont
}];

你可以使用:

  • Optima-BoldItalic
  • TimesNewRomanPS-BoldItalicMT
  • Baskerville-BoldItalic
  • HelveticaNeue-BoldItalic
  • TrebuchetMS-Bold
  • Helvetica-BoldOblique

您可以尝试下面的代码将您的文本设置为斜体和粗体。

 NSShadow *shadow = [[NSShadow alloc] init];
        shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
        shadow.shadowOffset = CGSizeMake(0, 1);
        [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                               [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
                                                               shadow, NSShadowAttributeName,
                                                               [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:21.0], NSFontAttributeName, nil]];
        [self setTitle:@"Title text"];

最新更新