有没有办法将导航栏中的标题更改为斜体,粗体和下划线,而不更改字体?

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

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

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

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

  • 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 }]; 

您可以使用 :

  • 舰,BOLDITALIC
  • TimesNewRomanPS-BoldItalicMT
  • 巴斯克维尔-BOLDITALIC
  • HelveticaNeue-BOLDITALIC
  • TrebuchetMS粗体
  • 黑体-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"];