我的应用程序有一个带有图像按钮的工具栏(UIButton 的子类);当用户打开粗体文本"可访问性选项时,不仅文本变为粗体,图像也随之变为粗体.
My application has a toolbar with image buttons on them (subclass of UIButton); when the user switches on the "Bold text" accessibility option, not only the text becomes bold but the images follow suit.
这是正常模式下的工具栏:
This is the toolbar in normal mode:
启用粗体"时:
这似乎是由我的 UIButton 子类引起的,它包含在下面.我正在使用此类在单击、禁用按钮等时应用图像色调颜色,并防止必须包含每个按钮的多个状态.为此,我使用 UIImageRenderingModeAlwaysTemplate
其中 据报道表现出这种观察到的行为.
It seems to be caused by my UIButton subclass, which is included below. I'm using this class to apply an image tint colour when the button is clicked, disabled, etc. and prevents having to include multiple states of each button. For that I'm using the UIImageRenderingModeAlwaysTemplate
which reportedly exhibits this observed behaviour.
我尝试取消选中界面构建器中的辅助功能"选项,但这根本没有效果.有没有办法解决这个问题?
I've tried to uncheck the "Accessibility" option in the interface builder, but that had no effect at all. Is there a way to fix this?
#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
感谢 Rufel 的回答 我得以解决我的问题,同时减少了我班级的代码:
Thanks to Rufel's answer I was able to fix my issue and reduce the code of my class at the same time:
#import "AppButton.h"
@interface AppButton ()
@property (readonly) UIImage *normalImage;
@end
@implementation AppButton
@synthesize highlightTintColor = _highlightTintColor;
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self initialize];
}
return self;
}
- (UIImage *)normalImage
{
return [self imageForState:UIControlStateNormal];
}
- (void)initialize
{
self.adjustsImageWhenHighlighted = NO;
// set disabled image
[self setImage:[self image:self.normalImage tintedWithColor:[UIColor colorWithRGBValue:RGBValueC9]] forState:UIControlStateDisabled];
}
- (void)setHighlightTintColor:(UIColor *)highlightTintColor
{
_highlightTintColor = highlightTintColor;
// update highlighted image
if (highlightTintColor) {
[self setImage:[self image:self.normalImage tintedWithColor:highlightTintColor] forState:UIControlStateHighlighted];
}
}
- (UIImage *)image:(UIImage *)image tintedWithColor:(UIColor *)tintColor
{
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0f);
// Tint image
[tintColor set];
UIRectFill(rect);
[image drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1.0f];
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
@end
这篇关于如何使用 UIImageRenderingModeAlwaysTemplate 防止粗体图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!