Skip to content

Commit

Permalink
add updates for toggling masked text and bump version number
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Senior committed Jul 24, 2017
1 parent a73d935 commit b8f8414
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 54 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion Classes/RSMaskedLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

@interface RSMaskedLabel : UILabel

@property (nonatomic) BOOL transparencyIsEnabled;
@property (nonatomic, getter=isMaskedTextEnabled) BOOL maskedTextEnabled;

@end
52 changes: 30 additions & 22 deletions Classes/RSMaskedLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 3 additions & 1 deletion Example/RSMaskedLabel/RSMaskedLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@

@interface RSMaskedLabel : UILabel

@end
@property (nonatomic, getter=isMaskedTextEnabled) BOOL maskedTextEnabled;

@end
78 changes: 49 additions & 29 deletions Example/RSMaskedLabel/RSMaskedLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion RSMaskedLabel.podspec
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit b8f8414

Please sign in to comment.