gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:32:16 27 4
gpt4 key购买 nike

我的应用程序有一个带有图像按钮的工具栏(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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com