Permalink
Browse files

Added contentImageWithBounds:options: method

This can be used to get an image for normal and highlighted hyperlink buttons.
  • Loading branch information...
1 parent c2c55c0 commit 5d96297b04a1137fe3a81a0388b2d5cea476554f @odrobnik odrobnik committed Mar 21, 2013
Showing with 38 additions and 1 deletion.
  1. +18 −1 Core/Source/DTAttributedTextContentView.h
  2. +20 −0 Core/Source/DTAttributedTextContentView.m
@@ -6,7 +6,7 @@
// Copyright 2011 Drobnik.com. All rights reserved.
//
-#import <CoreText/CoreText.h>
+#import "DTCoreTextLayoutFrame.h"
@class DTAttributedTextContentView;
@class DTCoreTextLayoutFrame;
@@ -308,3 +308,20 @@ typedef NSUInteger DTAttributedTextContentViewRelayoutMask;
@end
+
+/**
+ Methods for drawing the content view
+ */
+@interface DTAttributedTextContentView (Drawing)
+
+/**
+ Creates an image from a part of the receiver's content view
+ @param bounds The bounds of the content to draw
+ @param options The drawing options to apply when drawing
+ @see [DTCoreTextLayoutFrame drawInContext:options:] for a list of available drawing options
+ @returns A `UIImage` with the specified content
+ */
+- (UIImage *)contentImageWithBounds:(CGRect)bounds options:(DTCoreTextLayoutFrameDrawingOptions)options;
+
+@end
+
@@ -882,3 +882,23 @@ - (void)setDelegate:(id<DTAttributedTextContentViewDelegate>)delegate
@synthesize relayoutMask = _relayoutMask;
@end
+
+@implementation DTAttributedTextContentView (Drawing)
+
+- (UIImage *)contentImageWithBounds:(CGRect)bounds options:(DTCoreTextLayoutFrameDrawingOptions)options
+{
+ UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0);
+
+ CGContextRef context = UIGraphicsGetCurrentContext();
+
+ CGContextTranslateCTM(context, -bounds.origin.x, -bounds.origin.y);
+
+ [self.layoutFrame drawInContext:context options:options];
+
+ UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
+ UIGraphicsEndImageContext();
+
+ return image;
+}
+
+@end

0 comments on commit 5d96297

Please sign in to comment.