【问题标题】:How change Text color UITabBarItem selected? [duplicate]如何更改选定的文本颜色 UITabBarItem? [复制]
【发布时间】:2011-08-24 20:06:06
【问题描述】:

可能重复:
Setting text-color on selected tab bar item

你好,

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

非常感谢。

问候。

【问题讨论】:

    标签: objective-c uitabbar tabbar uitabbaritem


    【解决方案1】:

    将此添加到您的应用委托文件中 -

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

    【讨论】:

    • 感谢您的回复。这个特性也是需要实现的,但是我喜欢改变标签栏图标下的item中出现的文本颜色。这可能吗?
    • 谢谢。它看起来很棒,但我无法让它工作 - 控制台给出了诸如“CGContextSetStyle:无效上下文 0x0”之类的错误。有什么想法吗?
    【解决方案2】:

    要做到这一点,您必须覆盖 UITabBarController 类,但如果您是新手,我不会为此付出任何努力。在 iOS 开发中,最好学习iOS guidelines,因为最终您的应用会被 Apple 审核批准/拒绝。

    【讨论】:

      猜你喜欢
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多