Last active
January 31, 2019 16:14
-
-
Save SoundBlaster/6b1f2ec958f5f0510a2960d119435772 to your computer and use it in GitHub Desktop.
GradientTextView - simple subclass of UITextView to fill a drawing text with gradient. Just a proof-of-concept. Or you can just use UIColor from pattern image: [UIColor colorWithPatternImage:gradienImageWithSizeEqualToTextViewSize]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static inline UIImage *DrawLayerAsImage(CALayer *layer) { | |
UIImage *res = nil; | |
if (layer != nil) { | |
CGSize size = layer.bounds.size; | |
UIGraphicsBeginImageContextWithOptions(size, NO, 0); | |
CGContextRef ctx = UIGraphicsGetCurrentContext(); | |
[layer renderInContext:ctx]; | |
res = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
} | |
return res; | |
} | |
@interface GradientTextView : UITextView | |
@end | |
@interface GradientTextView () <UITextViewDelegate> | |
@property (nonatomic, strong) CAGradientLayer *gradientLayer; | |
@end | |
@implementation GradientTextView | |
- (void)awakeFromNib { | |
[super awakeFromNib]; | |
[self setupGradient]; | |
} | |
- (instancetype)initWithFrame:(CGRect)frame { | |
self = [super initWithFrame:frame]; | |
if (self != nil) { | |
[self setupGradient]; | |
} | |
return self; | |
} | |
- (void)setupGradient { | |
// gradient | |
CAGradientLayer *gradientLayer = [CAGradientLayer layer]; | |
gradientLayer.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor greenColor].CGColor]; | |
gradientLayer.locations = @[ @0.0f, @1.0f ]; | |
gradientLayer.startPoint = CGPointMake(0, 0); | |
gradientLayer.endPoint = CGPointMake(1, 0); | |
UIView *view = [[UIView alloc] initWithFrame:self.bounds]; | |
view.userInteractionEnabled = NO; | |
view.backgroundColor = [UIColor clearColor]; | |
[view.layer addSublayer:gradientLayer]; | |
[self addSubview:view]; | |
self.gradientLayer = gradientLayer; | |
self.delegate = self; | |
} | |
- (void)layoutSubviews { | |
[super layoutSubviews]; | |
self.gradientLayer.frame = CGRectOffset(self.bounds, 0, 0); | |
[self updateMask]; | |
} | |
- (void)updateMask { | |
CALayer *renderedLayer = self.subviews.firstObject.layer.sublayers.firstObject; | |
CGSize renderSize = renderedLayer.frame.size; | |
UIImage *image = DrawLayerAsImage(renderedLayer); | |
CALayer *maskLayer = [CALayer layer]; | |
maskLayer.frame = CGRectMake(0, 0, renderSize.width, renderSize.height); | |
maskLayer.contents = (id)image.CGImage; | |
self.gradientLayer.mask = maskLayer; | |
} | |
#pragma mark - <UITextViewDelegate> | |
-(void)textViewDidChange:(UITextView *)textView { | |
[self updateMask]; | |
} | |
// Fix for https://stackoverflow.com/a/26430239/602249 on the old iOS | |
/* | |
- (BOOL)respondsToSelector:(SEL)aSelector { | |
NSString * selectorName = NSStringFromSelector(aSelector); | |
if ([selectorName isEqualToString:@"customOverlayContainer"]) { | |
NSLog(@"preventing self.delegate == self crash"); | |
return NO; | |
} | |
return [UITextView instancesRespondToSelector:aSelector]; | |
}*/ | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment