From b8f8414fac4de47aa6be6bd2003fe8cc43a6774b Mon Sep 17 00:00:00 2001 From: Robin Senior Date: Mon, 24 Jul 2017 10:51:42 -0400 Subject: [PATCH] add updates for toggling masked text and bump version number --- CHANGELOG.md | 8 +++ Classes/RSMaskedLabel.h | 2 +- Classes/RSMaskedLabel.m | 52 ++++++++++-------- Example/RSMaskedLabel/RSMaskedLabel.h | 4 +- Example/RSMaskedLabel/RSMaskedLabel.m | 78 +++++++++++++++++---------- RSMaskedLabel.podspec | 2 +- 6 files changed, 92 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4ca08a..cac5830 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,3 +3,11 @@ ## 0.1.0 Initial release. + +## 0.2.0 + +Add support for Wide Color + +## 0.2.1 + +Add support for disabling text masking diff --git a/Classes/RSMaskedLabel.h b/Classes/RSMaskedLabel.h index 9b4cb2e..80d68d5 100644 --- a/Classes/RSMaskedLabel.h +++ b/Classes/RSMaskedLabel.h @@ -10,6 +10,6 @@ @interface RSMaskedLabel : UILabel -@property (nonatomic) BOOL transparencyIsEnabled; +@property (nonatomic, getter=isMaskedTextEnabled) BOOL maskedTextEnabled; @end diff --git a/Classes/RSMaskedLabel.m b/Classes/RSMaskedLabel.m index d06cbcb..75e2bc7 100644 --- a/Classes/RSMaskedLabel.m +++ b/Classes/RSMaskedLabel.m @@ -8,14 +8,10 @@ #import "RSMaskedLabel.h" -@interface RSMaskedLabel() -- (void) RS_commonInit; -- (void) RS_drawBackgroundInRect:(CGRect)rect; -@end - @implementation RSMaskedLabel { - UIColor* maskedBackgroundColor; + UIColor *_maskedBackgroundColor; + BOOL _maskedTextEnabled; } - (id)initWithFrame:(CGRect)frame @@ -32,40 +28,52 @@ - (id)initWithCoder:(NSCoder *)aDecoder return self; } -- (UIColor*) backgroundColor +- (UIColor*)backgroundColor { - return maskedBackgroundColor; + return _maskedBackgroundColor; } -- (void) setBackgroundColor:(UIColor *)backgroundColor +- (void)setBackgroundColor:(UIColor *)backgroundColor { - maskedBackgroundColor = backgroundColor; + _maskedBackgroundColor = backgroundColor; [self setNeedsDisplay]; } - (void)RS_commonInit { - maskedBackgroundColor = [super backgroundColor]; - [super setTextColor:[UIColor whiteColor]]; + _maskedBackgroundColor = [super backgroundColor]; [super setBackgroundColor:[UIColor clearColor]]; - [self setOpaque:NO]; - self.transparencyIsEnabled = YES; + self.opaque = NO; + self.maskedTextEnabled = YES; } - (void)setTextColor:(UIColor *)textColor { - // text color needs to be white for masking to work - if (!self.transparencyIsEnabled) { - - [super setTextColor:textColor]; + [super setTextColor:textColor]; + + self.maskedTextEnabled = false; +} + +-(void)setMaskedTextEnabled:(BOOL)maskedTextEnabled +{ + _maskedTextEnabled = maskedTextEnabled; + + if (_maskedTextEnabled) { + // text color needs to be white for masking to work + [super setTextColor:[UIColor whiteColor]]; } + + [self setNeedsDisplay]; +} + +- (BOOL)isMaskedTextEnabled +{ + return _maskedTextEnabled; } - (void)drawRect:(CGRect)rect { - - if (!self.transparencyIsEnabled) { - + if (!self.isMaskedTextEnabled) { [super drawRect:rect]; return; } @@ -114,7 +122,7 @@ - (void) RS_drawBackgroundInRect:(CGRect)rect // this is where you do whatever fancy drawing you want to do! CGContextRef context = UIGraphicsGetCurrentContext(); - [maskedBackgroundColor set]; + [_maskedBackgroundColor set]; CGContextFillRect(context, rect); } diff --git a/Example/RSMaskedLabel/RSMaskedLabel.h b/Example/RSMaskedLabel/RSMaskedLabel.h index 83d9f54..80d68d5 100644 --- a/Example/RSMaskedLabel/RSMaskedLabel.h +++ b/Example/RSMaskedLabel/RSMaskedLabel.h @@ -10,4 +10,6 @@ @interface RSMaskedLabel : UILabel -@end \ No newline at end of file +@property (nonatomic, getter=isMaskedTextEnabled) BOOL maskedTextEnabled; + +@end diff --git a/Example/RSMaskedLabel/RSMaskedLabel.m b/Example/RSMaskedLabel/RSMaskedLabel.m index 76748e2..75e2bc7 100644 --- a/Example/RSMaskedLabel/RSMaskedLabel.m +++ b/Example/RSMaskedLabel/RSMaskedLabel.m @@ -8,14 +8,10 @@ #import "RSMaskedLabel.h" -@interface RSMaskedLabel() -- (void) RS_commonInit; -- (void) RS_drawBackgroundInRect:(CGRect)rect; -@end - @implementation RSMaskedLabel { - UIColor* maskedBackgroundColor; + UIColor *_maskedBackgroundColor; + BOOL _maskedTextEnabled; } - (id)initWithFrame:(CGRect)frame @@ -32,77 +28,101 @@ - (id)initWithCoder:(NSCoder *)aDecoder return self; } -- (UIColor*) backgroundColor +- (UIColor*)backgroundColor { - return maskedBackgroundColor; + return _maskedBackgroundColor; } -- (void) setBackgroundColor:(UIColor *)backgroundColor +- (void)setBackgroundColor:(UIColor *)backgroundColor { - maskedBackgroundColor = backgroundColor; + _maskedBackgroundColor = backgroundColor; [self setNeedsDisplay]; } - (void)RS_commonInit { - maskedBackgroundColor = [super backgroundColor]; - [super setTextColor:[UIColor whiteColor]]; + _maskedBackgroundColor = [super backgroundColor]; [super setBackgroundColor:[UIColor clearColor]]; - [self setOpaque:NO]; + self.opaque = NO; + self.maskedTextEnabled = YES; } - (void)setTextColor:(UIColor *)textColor { - // text color needs to be white for masking to work + [super setTextColor:textColor]; + + self.maskedTextEnabled = false; +} + +-(void)setMaskedTextEnabled:(BOOL)maskedTextEnabled +{ + _maskedTextEnabled = maskedTextEnabled; + + if (_maskedTextEnabled) { + // text color needs to be white for masking to work + [super setTextColor:[UIColor whiteColor]]; + } + + [self setNeedsDisplay]; +} + +- (BOOL)isMaskedTextEnabled +{ + return _maskedTextEnabled; } - (void)drawRect:(CGRect)rect { + if (!self.isMaskedTextEnabled) { + [super drawRect:rect]; + return; + } + // Render into a temporary bitmap context at a max of 8 bits per component for subsequent CGImageMaskCreate operations UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0); - + [super drawRect:rect]; - + CGContextRef context = UIGraphicsGetCurrentContext(); CGImageRef image = CGBitmapContextCreateImage(context); UIGraphicsEndImageContext(); - + // Revert to normal graphics context for the rest of the rendering context = UIGraphicsGetCurrentContext(); - + CGContextConcatCTM(context, CGAffineTransformMake(1, 0, 0, -1, 0, CGRectGetHeight(rect))); - + // create a mask from the normally rendered text CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(image), CGImageGetHeight(image), CGImageGetBitsPerComponent(image), CGImageGetBitsPerPixel(image), CGImageGetBytesPerRow(image), CGImageGetDataProvider(image), CGImageGetDecode(image), CGImageGetShouldInterpolate(image)); - + CFRelease(image); image = NULL; - + // wipe the slate clean CGContextClearRect(context, rect); - + CGContextSaveGState(context); CGContextClipToMask(context, rect, mask); - + if (self.layer.cornerRadius != 0.0f) { CGPathRef path = CGPathCreateWithRoundedRect(rect, self.layer.cornerRadius, self.layer.cornerRadius, nil); CGContextAddPath(context, path); CGContextClip(context); CGPathRelease(path); } - + CFRelease(mask); mask = NULL; - + [self RS_drawBackgroundInRect:rect]; - + CGContextRestoreGState(context); } -- (void)RS_drawBackgroundInRect:(CGRect)rect +- (void) RS_drawBackgroundInRect:(CGRect)rect { // this is where you do whatever fancy drawing you want to do! CGContextRef context = UIGraphicsGetCurrentContext(); - - [maskedBackgroundColor set]; + + [_maskedBackgroundColor set]; CGContextFillRect(context, rect); } diff --git a/RSMaskedLabel.podspec b/RSMaskedLabel.podspec index e04b9e4..7cbf3c6 100644 --- a/RSMaskedLabel.podspec +++ b/RSMaskedLabel.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "RSMaskedLabel" - s.version = "0.2" + s.version = "0.2.1" s.summary = "RSMaskedLabel is a UILabel subclass that renders knocked-out text using an inverted mask." s.description = <<-DESC Simple library for creating knocked-out text. Can be used for making embossed effects. Subclasses UILabel for easy integration with iOS projects.