开发者

How change Text color UITabBarItem selected? [duplicate]

开发者 https://www.devze.com 2023-03-05 21:36 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Setting text-color on selected tab bar item
This question already has answers here: Closed 11 years ago.

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 p开发者_JAVA百科ulse it. ¿Is this posible?

Thank you very much.

Regards.


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];


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号