我有一个多视图控制器,我想所有的字体颜色设置为红色。
[[UINavigationBar appearance] setFont:[UIFont boldSystemFontOfSize:12.0]];
被抛出无法识别选择错误。
请给一个代码,以帮助解决以下。
我有一个多视图控制器,我想所有的字体颜色设置为红色。
[[UINavigationBar appearance] setFont:[UIFont boldSystemFontOfSize:12.0]];
被抛出无法识别选择错误。
请给一个代码,以帮助解决以下。
从雷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]];
或者,如果你喜欢用对象文字样式:
[[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],
}];
编辑为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]
}];
对于部署目标大于或等于到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];
在iOS 8+和斯威夫特这样做。 没有一个setTitleTextAttributes
的外观对象。 相反,这样做:
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : AppTheme.fontWithSize(18)]
我在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];
这个对我有用...
如果你需要做到这一点的斯威夫特,你可以创建一个扩展名为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]
}
}
}
}
然后,您可以设置颜色和字体是这样的:
navigationBar.titleColor = UIColor.redColor()
navigationBar.titleFont = UIFont.systemFontOfSize(12)
这可以用于自定义视图设置为一个单一的导航栏,而不是全局设置的
- (void)updateTitleWithString:(NSString *)title
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero];
[headerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[headerView setAutoresizesSubviews:YES];
CGFloat headFontSize = (IS_SYSTEM_DEVICE_IPAD ? 25.0f : 19.0f);
UIFont *headFont = [UIFont boldSystemFontOfSize: headFontSize ];
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setLineBreakMode:NSLineBreakByTruncatingTail];
CGSize size = [title boundingRectWithSize:CGSizeMake(190,headFontSize + 6) options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName : headFont, NSParagraphStyleAttributeName : style} context:nil].size;
headerView.frame = CGRectMake(0, 0,size.width,self.navigationController.navigationBar.frame.size.height);
float labelHeight = headFontSize + 6;
float labelYLoc = ( self.navigationController.navigationBar.frame.size.height - labelHeight ) / 2;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,labelYLoc, size.width,labelHeight)];
label.backgroundColor = [UIColor clearColor];
label.adjustsFontSizeToFitWidth = YES;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.font = headFont;
label.text = title;
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.4];
label.lineBreakMode = NSLineBreakByTruncatingTail;
label.shadowOffset = CGSizeMake(0,-1);
label.accessibilityLabel = @"<LABEL>";
[headerView addSubview:label];
self.navigationItem.titleView = headerView;
}
使用此行代码
UILabel *badge_Label=[[UILabel alloc]initWithFrame:CGRectMake(5,3, 15, 15)];
badge_Label.backgroundColor=[UIColor redcolor];
badge_Label.font=[UIFont systemFontOfSize:12];
[badge_Label setText:@"20"];
[self.navigationController.navigationBar addSubview:badgelabel];
我认为这将是对你有帮助。