2012-10-30 50 views
4

我的应用程序出现问题。我在UITabBar的其中一个选项卡上设置了一个徽章值。徽章值正确为红色,徽章值周围的圆圈正确为白色。问题是,文本的颜色是灰色的(160,160,160)。它与正常状态下的tabbaritem文字是相同的颜色,但我在应用程序代码中没有设置此颜色,我不知道此颜色来自哪里。 自从周以来我在整个网络中搜索了这个问题,但我找不到任何解决方案。我在处处找到的唯一答案是,无法更改徽章值文本的颜色。但如果这是不可能的,为什么它在我的应用程序中更改? 我希望有人能帮助我这个问题......UITabbarItem BadgeValue文本颜色


编辑2012年11月2日 - 代码

TabBarController的创作:

#import "ExtendedTabBarController.h" 
#import "configuration.h" 

@implementation ExtendedTabBarController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:207.0/255.0 green:70.0/255.0 blue:61.0/255.0 alpha:1], UITextAttributeTextColor, [UIFont fontWithName:@"KievitPro-Regular" size:10.0], UITextAttributeFont, nil] forState:UIControlStateSelected]; 

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1], UITextAttributeTextColor, [UIFont fontWithName:@"KievitPro-Regular" size:10.0], UITextAttributeFont, nil] forState:UIControlStateNormal]; 

    [self.tabBar sizeToFit]; 

    UIView *tabbarBackgroundColorView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0, self.view.bounds.size.width, 49)]; 
    [tabbarBackgroundColorView setBackgroundColor:[UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:1]]; 
    [self.tabBar insertSubview:tabbarBackgroundColorView atIndex:0]; 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
} 


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return UIInterfaceOrientationIsPortrait(interfaceOrientation); // only portrait orientation 

} 

/** 
* orientation for iOS6 
**/ 
-(NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

@end 

通话中的AppDelegate:

ExtendedTabBarController *tabBarController = [[ExtendedTabBarController alloc] init]; 
[self setTabBarController:tabBarController]; 
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"menu_bg"]]; 

// code for initialize View- and NavigationControllers... 

self.tabBarController.viewControllers = @[highlightsNavigationController, categoryNavigationController, searchNavigationController, favoritesNavigationController, imprintNavigationController]; 

self.window.rootViewController = self.tabBarController; 

[[UITabBar appearance] setSelectionIndicatorImage:[[UIImage alloc] init]]; 

设置徽章值:

int viewCount = 0; 
NSUserDefaults * defs = [NSUserDefaults standardUserDefaults]; 
NSDictionary * dict = [defs dictionaryRepresentation]; 
for (id key in dict) { 
    if([key rangeOfString:@"_highlighted"].location != NSNotFound && [[[dict objectForKey:key] objectAtIndex:0] isEqualToString:@"YES"]) { 
     viewCount++; 
    } 
} 
UITabBarItem *tbi = (UITabBarItem *)[self.tabBarController.tabBar.items objectAtIndex:3]; 
if(viewCount <= 0) { 
    tbi.badgeValue = nil; 
} else { 
    tbi.badgeValue = nil; 
    tbi.badgeValue = [NSString stringWithFormat:@"%d", viewCount]; 
} 

代码覆盖的UILabel:

// -- file: UILabel+VerticalAlign.h 
#pragma mark VerticalAlign 
@interface UILabel (VerticalAlign) 
- (void)alignTop; 
- (void)alignBottom; 
- (void)awakeFromNib; 
-(id)initWithFrame:(CGRect)frame; 
@end 


#import "UILabel+VerticalAlign.h" 

// -- file: UILabel+VerticalAlign.m 
@implementation UILabel (VerticalAlign) 
- (void)alignTop { 
    CGSize fontSize = [self.text sizeWithFont:self.font]; 
    double finalHeight = fontSize.height * self.numberOfLines; 
    double finalWidth = self.frame.size.width; //expected width of label 
    CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode]; 
    int newLinesToPad = (finalHeight - theStringSize.height)/fontSize.height; 
    for(int i=0; i<newLinesToPad; i++) 
     self.text = [self.text stringByAppendingString:@"\n "]; 
} 

- (void)alignBottom { 
    CGSize fontSize = [self.text sizeWithFont:self.font]; 
    double finalHeight = fontSize.height * self.numberOfLines; 
    double finalWidth = self.frame.size.width; //expected width of label 
    CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode]; 
    int newLinesToPad = (finalHeight - theStringSize.height)/fontSize.height; 
    for(int i=0; i<newLinesToPad; i++) 
     self.text = [NSString stringWithFormat:@" \n%@",self.text]; 
} 
- (void)awakeFromNib 
{ 
    [super awakeFromNib]; 
    [self setFont:[UIFont fontWithName:@"KievitPro-Regular" size:12.0]]; 
} 

-(id)initWithFrame:(CGRect)frame 
{ 
    id result = [super initWithFrame:frame]; 
    if (result) { 
     [self setFont:[UIFont fontWithName:@"KievitPro-Regular" size:12.0]]; 
    } 
    return result; 
} 

@end 
+0

你有屏幕截图吗?使用Cmd + Shift + 4快照,并将其添加到您的问题。谢谢 – WDUK

+0

我添加了该帖子的链接... –

+0

谢谢。不知道为什么会发生这种情况。你能分享一下你如何创建你的UITabBar,使它看起来像它一样,也许缺少一些东西? – WDUK

回答

2

我发现我自己对我的问题的解决方案:

我必须删除从覆盖的UILabel以下行:

- (void)awakeFromNib 
{ 
    [super awakeFromNib]; 
    [self setFont:[UIFont fontWithName:@"KievitPro-Regular" size:12.0]]; 
} 

-(id)initWithFrame:(CGRect)frame 
{ 
    id result = [super initWithFrame:frame]; 
    if (result) { 
     [self setFont:[UIFont fontWithName:@"KievitPro-Regular" size:12.0]]; 
    } 
    return result; 
} 

也许有人可以解释我,为什么这行代码会更改徽章值的文本颜色,然后才能关闭此帖子?

+0

奇数。我不知道为什么会改变颜色?很高兴你让它工作。 – Josiah

+0

我玩了很多选项,我必须改变颜色,但徽章标签不处理它们。当我添加这两种方法时,徽章标签中的文字为灰色(160.0,160.0,160.0),当我将它们移除时,颜色为白色。但是不可能给文本一个明确的颜色。 我也尝试改变TabBar视图的背景颜色,文本颜色和突出显示的TabBar项目颜色,没有任何其他影响。 所以看来,这是一个不想要的副作用。 我想,这个问题可以被回答,如果没有人有想法,这是怎么回事。 –

+0

我唯一能想到的就是这里的字体。由于这两种方法不设置字体颜色,因此默认情况下KievitPro-Regular可能为灰色。我不确定。 – Josiah

0

而不是使用一个类别设置默认的UILabel字体,使用UILabel的外观的方法来设置字体:

[[UILabel appearance] setFont:[UIFont fontWithName:@"KievitPro-Regular" size:12.0]]; 

当我这个测试的徽章文本表现为正常的白色。

+0

不会使所有* UILabels在整个应用程序中使用12像素高的字体? –

+0

@BenClayton它确实改变了应用中所有UILabel的字体大小。但是,OP的创建类别来覆盖字体的方法具有相同的副作用。我也看了,也找不到改变所有UILabels字体的方法,而且不改变字体大小。 – robhasacamera