Created
September 16, 2014 20:24
-
-
Save HeidiHansen/8c271df9457e82ba581e to your computer and use it in GitHub Desktop.
08. THTextOverlay.m
This file contains 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
#import "THTextOverlay.h" | |
@implementation THTextOverlay | |
-(instancetype)init | |
{ | |
return [self initWithImageText:@"DEFAULT TEXT" | |
Font:[UIFont systemFontOfSize:20] | |
FontSize:20 | |
TextAlignment:NSTextAlignmentLeft | |
ForViewFrameToDrawIn:CGRectMake(0,0,100,100)]; | |
} | |
-(instancetype)initWithImageText:(NSString *)imageText | |
Font:(UIFont *)font | |
FontSize:(CGFloat)textSize | |
TextAlignment:(NSTextAlignment)textAlignment | |
ForViewFrameToDrawIn:(CGRect)viewFrameToDrawIn | |
{ | |
self = [super init]; | |
if (self) { | |
_imageText = imageText; | |
_font = font; | |
_textSize = textSize; | |
_viewFrameToDrawIn = viewFrameToDrawIn; | |
_textAlignment = textAlignment; | |
} | |
return self; | |
} | |
-(UIImage *)imageFromText | |
{ | |
UIColor *textColor = [UIColor colorWithRed:1.0 | |
green:1.0 | |
blue:1.0 | |
alpha:1.0]; | |
UIFont *font = [UIFont systemFontOfSize:self.textSize]; | |
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; | |
paragraphStyle.alignment = NSTextAlignmentRight; | |
NSDictionary *attr = @{NSForegroundColorAttributeName:textColor, | |
NSFontAttributeName:font, | |
NSParagraphStyleAttributeName:paragraphStyle}; | |
CGSize thetextSize = [self.imageText sizeWithAttributes:attr]; | |
CGRect textRect = CGRectMake(self.viewFrameToDrawIn.origin.x, self.viewFrameToDrawIn.origin.y, thetextSize.width, thetextSize.height); | |
// Create the image | |
UIGraphicsBeginImageContextWithOptions(textRect.size , NO , 0.0f); | |
CGContextClearRect(UIGraphicsGetCurrentContext(), textRect); | |
[self.imageText drawInRect:textRect withAttributes:attr]; | |
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return resultImage; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment