如何更改选定的UITabBarItem 文本颜色?

发布于 2024-11-07 08:24:56 字数 294 浏览 3 评论 0原文

可能的重复:
在选定的选项卡栏项目上设置文本颜色

你好,

我我正在为 iPhone 使用 Objective C 进行编程。我希望在脉冲时更改选项卡栏项目的文本颜色。这可能吗?

非常感谢。

问候。

Possible Duplicate:
Setting text-color on selected tab bar item

Hello,

I'm programming in Objective C for iPhone. I wish change the text color of Tab Bar item when I pulse it. ¿Is this posible?

Thank you very much.

Regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我偏爱纯白色 2024-11-14 08:24:56

将其添加到您的应用程序委托文件中 -

 @interface UITabBar (ColorExtensions)
 - (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur;
 @end

 @interface UITabBarItem (Private)
 @property(retain, nonatomic) UIImage *selectedImage;
 - (void)_updateView;
 @end

 @implementation UITabBar (ColorExtensions)

 - (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur
 {
 CGColorRef cgColor = [color CGColor];
 CGColorRef cgShadowColor = [shadowColor CGColor];
 for (UITabBarItem *item in [self items])
 if ([item respondsToSelector:@selector(selectedImage)] &&
 [item respondsToSelector:@selector(setSelectedImage:)] &&
 [item respondsToSelector:@selector(_updateView)])
 {
 CGRect contextRect;
 contextRect.origin.x = 0.0f;
 contextRect.origin.y = 0.0f;
 contextRect.size = [[item selectedImage] size];
 // Retrieve source image and begin image context
 UIImage *itemImage = [item image];
 CGSize itemImageSize = [itemImage size];
 CGPoint itemImagePosition; 
 itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
 itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);
 UIGraphicsBeginImageContext(contextRect.size);
 CGContextRef c = UIGraphicsGetCurrentContext();
 // Setup shadow
 CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor);

 // Setup transparency layer and clip to mask
 CGContextBeginTransparencyLayer(c, NULL);
 CGContextScaleCTM(c, 1.0, -1.0);
 CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [itemImage CGImage]);

 //Setup the gradient...    
 //CGFloat components[8] = {0.0,0.4,1.0,0.2,0.0,0.6,1.0,1.0};
 //CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();  
 //CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 2);
 //CGContextDrawLinearGradient(c, colorGradient,CGPointZero,CGPointMake(0,contextRect.size.height),0);


 // Fill and end the transparency layer
 CGContextSetFillColorWithColor(c, cgColor);
 contextRect.size.height = -contextRect.size.height;
 CGContextFillRect(c, contextRect);
 CGContextEndTransparencyLayer(c);



 // Set selected image and end context
 [item setSelectedImage:UIGraphicsGetImageFromCurrentImageContext()];
 UIGraphicsEndImageContext();
 // Update the view
 [item _updateView];



 }

 }

然后您可以为应用程序中的选项卡栏提供颜色,并使用此代码完成启动 -

[[tabbarcontroller tabBar] recolorItemsWithColor:[UIColor colorWithRed:0.6640 green:0.1992 blue:0.1992 alpha:1.0] shadowColor:[UIColor clearColor] shadowOffset:CGSizeMake(0.0f, -1.0f) shadowBlur:3.0f];

Add this in your app delegate file -

 @interface UITabBar (ColorExtensions)
 - (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur;
 @end

 @interface UITabBarItem (Private)
 @property(retain, nonatomic) UIImage *selectedImage;
 - (void)_updateView;
 @end

 @implementation UITabBar (ColorExtensions)

 - (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur
 {
 CGColorRef cgColor = [color CGColor];
 CGColorRef cgShadowColor = [shadowColor CGColor];
 for (UITabBarItem *item in [self items])
 if ([item respondsToSelector:@selector(selectedImage)] &&
 [item respondsToSelector:@selector(setSelectedImage:)] &&
 [item respondsToSelector:@selector(_updateView)])
 {
 CGRect contextRect;
 contextRect.origin.x = 0.0f;
 contextRect.origin.y = 0.0f;
 contextRect.size = [[item selectedImage] size];
 // Retrieve source image and begin image context
 UIImage *itemImage = [item image];
 CGSize itemImageSize = [itemImage size];
 CGPoint itemImagePosition; 
 itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
 itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);
 UIGraphicsBeginImageContext(contextRect.size);
 CGContextRef c = UIGraphicsGetCurrentContext();
 // Setup shadow
 CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor);

 // Setup transparency layer and clip to mask
 CGContextBeginTransparencyLayer(c, NULL);
 CGContextScaleCTM(c, 1.0, -1.0);
 CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [itemImage CGImage]);

 //Setup the gradient...    
 //CGFloat components[8] = {0.0,0.4,1.0,0.2,0.0,0.6,1.0,1.0};
 //CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();  
 //CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 2);
 //CGContextDrawLinearGradient(c, colorGradient,CGPointZero,CGPointMake(0,contextRect.size.height),0);


 // Fill and end the transparency layer
 CGContextSetFillColorWithColor(c, cgColor);
 contextRect.size.height = -contextRect.size.height;
 CGContextFillRect(c, contextRect);
 CGContextEndTransparencyLayer(c);



 // Set selected image and end context
 [item setSelectedImage:UIGraphicsGetImageFromCurrentImageContext()];
 UIGraphicsEndImageContext();
 // Update the view
 [item _updateView];



 }

 }

and then you can give color to your tab bar in application did finish launcing with this code -

[[tabbarcontroller tabBar] recolorItemsWithColor:[UIColor colorWithRed:0.6640 green:0.1992 blue:0.1992 alpha:1.0] shadowColor:[UIColor clearColor] shadowOffset:CGSizeMake(0.0f, -1.0f) shadowBlur:3.0f];
飞烟轻若梦 2024-11-14 08:24:56

要做到这一点,您必须覆盖 UITabBarController 类,但如果您是新手,我不会在这方面做出任何努力。在 iOS 开发中,最好学习 iOS 指南,因为最后你的应用程序将被苹果审核并批准/拒绝。

To make it you must overwrite the UITabBarController class, but if you're newbie I wouldn´t make any effort on that. In iOS development it's better to learn the iOS guidelines, because at last your app will be reviewed an approved/denied by Apple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文