Skip to content

Instantly share code, notes, and snippets.

@k06a
Created December 19, 2016 15:31
Show Gist options
  • Save k06a/bcaf413c0e6fb794e53e33ed96a63a09 to your computer and use it in GitHub Desktop.
Save k06a/bcaf413c0e6fb794e53e33ed96a63a09 to your computer and use it in GitHub Desktop.
UIView to replace UILabel with ability to super fast change text color
@interface LabelMaskedView : UIView
@property (strong, nonatomic) UIView *maskView;
@property (assign, nonatomic) CGSize cachedIntrisincContentSize;
@end
@implementation LabelMaskedView
- (instancetype)initWitLabel:(UIView *)maskView {
self = [super initWithFrame:maskView.frame];
if (self) {
maskView.layer.rasterizationScale = [UIScreen mainScreen].scale;
maskView.layer.shouldRasterize = YES;
_maskView = maskView;
self.layer.mask = (id)self.maskView.layer;
}
return self;
}
- (CGSize)intrinsicContentSize {
return self.maskView.intrinsicContentSize;
}
- (void)layoutSubviews {
[super layoutSubviews];
if (!CGRectEqualToRect(self.maskView.bounds, self.bounds)) {
self.maskView.frame = self.bounds;
}
}
- (void)setTextColor:(UIColor *)textColor {
[super setBackgroundColor:textColor];
}
#pragma mark Forward all unrecognized selectors to mask view
- (BOOL)respondsToSelector:(SEL)aSelector {
return [super respondsToSelector:aSelector] || [self.maskView respondsToSelector:aSelector];
}
- (id)forwardingTargetForSelector:(SEL)aSelector {
return self.maskView;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment