ios - 如何使用 UIImage RenderingMode AlwaysTemplate 防止粗体图像

标签 ios objective-c interface uibutton

我的应用程序有一个带有图像按钮的工具栏(UIButton 的子类);当用户打开“粗体文本”辅助功能选项时,不仅文本会变成粗体,图像也会随之变粗。

这是正常模式下的工具栏:

Toolbar in normal mode

启用“粗体文本”时:

Toolbar in bold mode

这似乎是由我的 UIButton 子类引起的,它包含在下面。我正在使用此类在单击、禁用按钮等时应用图像色调颜色,并避免必须包含每个按钮的多个状态。为此,我使用了 UIImageRenderingModeAlwaysTemplate reportedly表现出这种观察到的行为。

我试图取消选中界面生成器中的“辅助功能”选项,但这根本没有效果。有办法解决这个问题吗?

#import "AppButton.h"

@implementation AppButton

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.adjustsImageWhenHighlighted = NO;

    [self setImage:[[self imageForState:UIControlStateNormal] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
}

- (void)updateButtonView
{
    if (!self.enabled) {
        self.imageView.tintColor = [UIColor colorWithRGBValue:RGBValueC9];
    } else if (self.highlighted) {
        self.imageView.tintColor = self.highlightTintColor;
    } else {
        self.imageView.tintColor = self.tintColor;
    }
}

- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    [self updateButtonView];
}

- (void)setEnabled:(BOOL)enabled
{
    [super setEnabled:enabled];
    [self updateButtonView];
}

- (void)setTintColor:(UIColor *)tintColor
{
    [super setTintColor:tintColor];
    [self updateButtonView];
}

@end

最佳答案

我建议您使用自定义类别来为图像按钮着色。这是一个简单的实现:

UIImage+TintImage.h

@interface UIImage (TintImage)
- (UIImage *)imageTintedWithColor:(UIColor *)tintColor;
@end

UIImage+TintImage.m

#import "UIImage+TintImage.h"

@implementation UIImage (TintImage)
- (UIImage *)imageTintedWithColor:(UIColor *)tintColor
{
    if (tintColor == nil) {
        tintColor = [UIColor whiteColor];
    }

    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);

    // Tint image
    [tintColor set];
    UIRectFill(rect);
    [self drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1.0f];
    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return tintedImage;
}
@end

要使用它,只需导入"UIImage+TintImage.h",然后执行以下操作:

UIImage *originalImage = [UIImage imageNamed:@"icn-menu"];
UIImage *tintedImage = [originalImage imageTintedWithColor:[UIColor blueColor]];
UIButton *homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[homeButton setImage:originalImage forState:UIControlStateNormal];
[homeButton setImage:tintedImage forState:UIControlStateHighlighted];

button showcase

关于ios - 如何使用 UIImage RenderingMode AlwaysTemplate 防止粗体图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28935349/

相关文章:

ios - UIGetScreenImage() 的替代方法

iphone - 使用开始时间和持续时间计算结束时间-iOS

ios - 当我不创建 NSTimer 对象时如何使计时器无效

c - 带有 getifaddrs 的 MAC 地址

objective-c - 无法从 UIAlertView 更改 View Controller

iphone - 如何使用查找和替换在 Xcode 中用单个新行替换多个新行

ios - 为什么我不能操作我的 xib 文件中的对象?

ios - 如何在 main.storyboard 中显示屏幕外的 UIView?

android - 如何使用 Activity 和 Fragment 之间的接口(interface)双向发送数据

ios - iPhone 应用编程 : best element from the interface builder to display calculated values