【发布时间】:2014-05-12 06:58:35
【问题描述】:
我使用 UIAppearance 在导航栏中更改标题的属性,如下所示:
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [MM mainTitleColor]}];
但是我还没有找到使文本带下划线或斜体的方法,有没有办法在不改变字体的情况下做到这一点?
【问题讨论】:
标签: ios
我使用 UIAppearance 在导航栏中更改标题的属性,如下所示:
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [MM mainTitleColor]}];
但是我还没有找到使文本带下划线或斜体的方法,有没有办法在不改变字体的情况下做到这一点?
【问题讨论】:
标签: ios
没有。除非字体更改,否则您无法更改这些属性。因为外观代理下的可用键是
更改这些属性以自定义 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
}];
你可以使用:
【讨论】:
您可以尝试下面的代码使您的文本斜体和粗体。
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"];
【讨论】: