【问题标题】:Is there a way to change title in navigation bar to Italic, Bold and underlined without changing font?有没有办法在不改变字体的情况下将导航栏中的标题更改为斜体、粗体和下划线?
【发布时间】:2014-05-12 06:58:35
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios


    【解决方案1】:

    没有。除非字体更改,否则您无法更改这些属性。因为外观代理下的可用键是

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

    【讨论】:

      【解决方案2】:

      试试这个。

      您必须通过添加 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-粗体
      • Helvetica-BoldOblique

      【讨论】:

        【解决方案3】:

        您可以尝试下面的代码使您的文本斜体和粗体。

         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"];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-25
          • 1970-01-01
          • 2021-04-28
          • 2020-12-22
          • 1970-01-01
          • 2011-03-03
          • 1970-01-01
          • 2011-12-05
          相关资源
          最近更新 更多