car*_*onr 90 uinavigationbar ios5 uiappearance
我有一个多个视图控制器,我想将所有字体颜色设置为红色.
[[UINavigationBar appearance] setFont:[UIFont boldSystemFontOfSize:12.0]];
Run Code Online (Sandbox Code Playgroud)
抛出一个无法识别的选择器错误.
请提供代码,以帮助解决以下问题.
Tig*_*ing 207
来自Ray Wenderlich:
http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5
// Customize the title text for *all* UINavigationBars
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Arial-Bold" size:0.0],
UITextAttributeFont,
nil]];
Run Code Online (Sandbox Code Playgroud)
或者如果您更喜欢对象文字样式:
[[UINavigationBar appearance] setTitleTextAttributes:@{
UITextAttributeTextColor: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
UITextAttributeTextShadowColor: [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeFont: [UIFont fontWithName:@"Arial-Bold" size:0.0],
}];
Run Code Online (Sandbox Code Playgroud)
编辑iOS 7及以下版本
UITextAttributes作为iOS 7弃用,您可以使用以下内容:
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithWhite:.0f alpha:1.f];
shadow.shadowOffset = CGSizeMake(0, -1);
[[UINavigationBar appearance] setTitleTextAttributes:@{
NSForegroundColorAttributeName: [UIColor whiteColor],
NSShadowAttributeName: shadow,
NSFontAttributeName: [UIFont fontWithName:@"Arial-Bold" size:15.0f]
}];
Run Code Online (Sandbox Code Playgroud)
Rob*_*ert 23
对于大于或等于iOS 6的部署目标,您应该使用NSShadow:
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor lightGrayColor];
shadow.shadowOffset = CGSizeMake(0, -2);
NSDictionary * navBarTitleTextAttributes =
@{ NSForegroundColorAttributeName : [UIColor redColor],
NSShadowAttributeName : shadow,
NSFontAttributeName : [UIFont systemFontOfSize:14] };
[[UINavigationBar appearance] setTitleTextAttributes:navBarTitleTextAttributes];
Run Code Online (Sandbox Code Playgroud)

Mat*_*ros 19
在iOS 8+和Swift中执行此操作.没有一个setTitleTextAttributes对外观对象.相反,这样做:
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : AppTheme.fontWithSize(18)]
Run Code Online (Sandbox Code Playgroud)
我这样做是通过在AppDelegate.m类中添加几行代码didFinishLaunchingWithOptions方法:使用此代码:
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:255.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0],UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
Run Code Online (Sandbox Code Playgroud)
这个对我有用...
如果需要在Swift中执行此操作,可以为UINavigationBar创建扩展,以允许您获取或设置这些设置.
extension UINavigationBar {
var titleColor: UIColor? {
get {
if let attributes = self.titleTextAttributes {
return attributes[NSForegroundColorAttributeName] as? UIColor
}
return nil
}
set {
if let value = newValue {
self.titleTextAttributes = [NSForegroundColorAttributeName: value]
}
}
}
var titleFont: UIFont? {
get {
if let attributes = self.titleTextAttributes {
return attributes[NSFontAttributeName] as? UIFont
}
return nil
}
set {
if let value = newValue {
self.titleTextAttributes = [NSFontAttributeName: value]
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以设置颜色和字体,如下所示:
navigationBar.titleColor = UIColor.redColor()
navigationBar.titleFont = UIFont.systemFontOfSize(12)
Run Code Online (Sandbox Code Playgroud)