Objective c UItabaritem BadgeValue文本颜色
我的应用程序有问题。我在UITabBar中的一个选项卡上设置了一个徽章值。徽章值正确显示为红色,徽章值周围的圆圈正确显示为白色。问题是,文本的颜色是灰色的(160160160)。它的颜色与正常状态tabbaritem文本的颜色相同,但我在应用程序代码中没有设置此颜色,我不知道此颜色来自何处。 几个星期以来,我一直在网上搜索这个问题,但我找不到任何解决办法。我在各处找到的唯一答案是,不可能更改徽章值文本的颜色。但如果不可能,为什么在我的应用程序中会更改它? 我希望有人能帮我解决这个问题Objective c UItabaritem BadgeValue文本颜色,objective-c,ios,uitabbar,uicolor,uitabbaritem,Objective C,Ios,Uitabbar,Uicolor,Uitabbaritem,我的应用程序有问题。我在UITabBar中的一个选项卡上设置了一个徽章值。徽章值正确显示为红色,徽章值周围的圆圈正确显示为白色。问题是,文本的颜色是灰色的(160160160)。它的颜色与正常状态tabbaritem文本的颜色相同,但我在应用程序代码中没有设置此颜色,我不知道此颜色来自何处。 几个星期以来,我一直在网上搜索这个问题,但我找不到任何解决办法。我在各处找到的唯一答案是,不可能更改徽章值文本的颜色。但如果不可能,为什么在我的应用程序中会更改它? 我希望有人能帮我解决这个问题 就像颜
- 就像颜色在我的应用程序中一样
- 喜欢的颜色通常应该是
编辑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];
}
int viewCount=0;
NSUserDefaults*defs=[NSUserDefaults standardUserDefaults];
NSDictionary*dict=[defs Dictionary表示];
for(dict中的id键){
if([key rangeOfString:@“_highlighted”].location!=NSNotFound&[[dict objectForKey:key]objectAtIndex:0]IsequalString:@“YES”]){
viewCount++;
}
}
uitabaritem*tbi=(uitabaritem*)[self.tabBarController.tabBar.items objectAtIndex:3];
如果(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
- (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;
}
也许有人可以向我解释,为什么在我们可以关闭此帖子之前,这几行会更改徽章值的文本颜色?使用UILabel的外观方法设置字体,而不是使用类别设置默认UILabel字体:
[[UILabel appearance] setFont:[UIFont fontWithName:@"KievitPro-Regular" size:12.0]];
当我测试这个时,徽章的文本显示为正常的白色。你有截图吗?使用Cmd+Shift+4抓拍,并将其添加到你的问题中。谢谢。不知道为什么会发生这种情况。你能分享一下你是如何创建你的UITabBar的,使它看起来像是这样的吗?可能缺少什么?我已经添加了代码snippets我如何创建TabBarController。它tabBar本身由TabBarController自动初始化…我创建了一个新的“tabbed应用程序”项目和粘贴尽可能多的你的代码。对不起,我的徽章文本变成白色,完全正常。我不能使它看起来像你的。所以我猜你的问题不在你发布的代码中。我现在发现,灰色文本颜色的原因是被覆盖的UILabel,但我不明白,为什么…我覆盖了它要进行垂直对齐。我将代码添加到主贴子中。奇怪。我不知道为什么会更改颜色?很高兴你能让它正常工作。我使用了很多必须更改颜色的选项,但徽章标签无法处理它们。当我添加这两种方法时,徽章标签中的文本为灰色(160.0、160.0、160.0),当我删除它们时,颜色是白色的。但无法为文本提供明确的颜色。我还尝试更改选项卡栏视图的背景色、文本颜色和选项卡栏项的高亮显示颜色,但没有任何其他效果。因此,这似乎是一个不想要的副作用。我认为,这个问题可以解决正如我所回答的,如果没有人知道这是怎么回事。我唯一能想到的就是这里的字体。因为这两种方法不设置字体颜色,也许KievitPro Regular默认为灰色。我不确定。只是好奇,但是如果你不将视图插入UITabBar中,给它一个自定义颜色,它还会发生吗?你试过使用属性来定制您的UI元素?KivitPro的默认字体颜色不能作为原因,因为我还使用“仅包含此方法”来测试此行为,而不覆盖字体,结果相同…这不会使整个应用程序中的所有UILabels都使用12像素高的字体吗?@BenClayton它确实会更改所有UILabels I的字体大小然而,OP创建一个类别来覆盖字体的方法也有同样的副作用。我也查看了一下,在不改变字体大小的情况下,找不到改变所有UILabel字体的方法。