Permalink
Browse files

Implemented simple text box support. block-level elements can have a …

…padding and background color, so far only 1 level deep supported.
  • Loading branch information...
odrobnik committed Mar 4, 2012
1 parent e803b1b commit 8004a34f0595f8b76a255394020b17ecf6ab1e64
@@ -15,6 +15,7 @@
@class DTAttributedTextContentView;
@class DTCoreTextLayoutFrame;
+@class DTTextBlock;
/**
Protocol to provide custom views for elements in an DTAttributedTextContentView. Also the delegate gets notified once the text view has been drawn.
@@ -38,6 +39,20 @@
- (void)attributedTextContentView:(DTAttributedTextContentView *)attributedTextContentView didDrawLayoutFrame:(DTCoreTextLayoutFrame *)layoutFrame inContext:(CGContextRef)context;
+/**
+ Called before the text belonging to a text block is drawn.
+
+ This gives the developer an opportunity to draw a custom background below a text block.
+
+ @param attributedTextContentView The content view that drew a layout frame
+ @param textBlock The text block
+ @param rect The frame within the content view's coordinate system that will be drawn into
+ @param context The graphics context that will be drawn into
+ @param layoutFrame The layout frame that will be drawn for
+ @param returns `YES` is the standard fill of the text block should be drawn, `NO` if it should not
+ */
+- (BOOL)attributedTextContentView:(DTAttributedTextContentView *)attributedTextContentView shouldDrawBackgroundForTextBlock:(DTTextBlock *)textBlock frame:(CGRect)frame context:(CGContextRef)context forLayoutFrame:(DTCoreTextLayoutFrame *)layoutFrame;
+
/**
@name Providing Custom Views for Content
*/
@@ -51,6 +51,7 @@ @interface DTAttributedTextContentView ()
unsigned int delegateSupportsCustomViewsForLinks:1;
unsigned int delegateSupportsGenericCustomViews:1;
unsigned int delegateSupportsNotificationAfterDrawing:1;
+ unsigned int delegateSupportsNotificationBeforeTextBoxDrawing:1;
} _delegateFlags;
__unsafe_unretained id <DTAttributedTextContentViewDelegate> _delegate;
@@ -657,6 +658,21 @@ - (DTCoreTextLayoutFrame *)layoutFrame
rect.size.height = CGFLOAT_OPEN_HEIGHT; // necessary height set as soon as we know it.
_layoutFrame = [theLayouter layoutFrameWithRect:rect range:NSMakeRange(0, 0)];
+
+ if (_delegateFlags.delegateSupportsNotificationBeforeTextBoxDrawing)
+ {
+ DTAttributedTextContentView *weakself = self;
+
+ [_layoutFrame setTextBlockHandler:^(DTTextBlock *textBlock, CGRect frame, CGContextRef context, BOOL *shouldDrawDefaultBackground) {
+ BOOL result = [weakself->_delegate attributedTextContentView:weakself shouldDrawBackgroundForTextBlock:textBlock frame:frame context:context forLayoutFrame:weakself->_layoutFrame];
+
+ if (shouldDrawDefaultBackground)
+ {
+ *shouldDrawDefaultBackground = result;
+ }
+
+ }];
+ }
}
}
}
@@ -723,6 +739,7 @@ - (void)setDelegate:(id<DTAttributedTextContentViewDelegate>)delegate
_delegateFlags.delegateSupportsCustomViewsForLinks = [_delegate respondsToSelector:@selector(attributedTextContentView:viewForLink:identifier:frame:)];
_delegateFlags.delegateSupportsGenericCustomViews = [_delegate respondsToSelector:@selector(attributedTextContentView:viewForAttributedString:frame:)];
_delegateFlags.delegateSupportsNotificationAfterDrawing = [_delegate respondsToSelector:@selector(attributedTextContentView:didDrawLayoutFrame:inContext:)];
+ _delegateFlags.delegateSupportsNotificationBeforeTextBoxDrawing = [_delegate respondsToSelector:@selector(attributedTextContentView:shouldDrawBackgroundForTextBlock:frame:context:forLayoutFrame:)];
if (!_delegateFlags.delegateSupportsCustomViewsForLinks && !_delegateFlags.delegateSupportsGenericCustomViews)
{
View
@@ -1,16 +1,31 @@
-
#if TARGET_OS_IPHONE
#import <CoreText/CoreText.h>
#endif
// global constants
#import "DTCoreTextConstants.h"
+// DTColor is UIColor on iOS, NSColor on Mac
+#import "DTColor+HTML.h"
+
+// DTImage is UIImage on iOS, NSImage on Mac
+#import "DTImage+HTML.h"
+
+// DTEdgeInsets is UIEdgeInsets on iOS, NSEdgeInsets on Mac
+#if TARGET_OS_IPHONE
+#define DTEdgeInsets UIEdgeInsets
+#define DTEdgeInsetsMake(a, b, c, d) UIEdgeInsetsMake(a, b, c, d)
+#else
+#define DTEdgeInsets NSEdgeInsets
+#define DTEdgeInsetsMake(a, b, c, d) NSEdgeInsetsMake(a, b, c, d)
+#endif
+
// common utilities
#import "CGUtils.h"
// common classes
#import "DTCSSListStyle.h"
+#import "DTTextBlock.h"
#import "DTCSSStylesheet.h"
#import "DTCoreText.h"
#import "DTCoreTextFontDescriptor.h"
@@ -19,6 +34,7 @@
#import "NSCharacterSet+HTML.h"
#import "NSData+DTBase64.h"
#import "NSScanner+HTML.h"
+#import "NSMutableString+HTML.h"
#import "NSString+CSS.h"
#import "NSString+HTML.h"
#import "NSString+Paragraphs.h"
@@ -28,11 +44,6 @@
#import "NSAttributedString+SmallCaps.h"
#import "NSAttributedString+DTCoreText.h"
-// DTColor is UIColor on iOS, NSColor on Mac
-#import "DTColor+HTML.h"
-
-// DTImage is UIImage on iOS, NSImage on Mac
-#import "DTImage+HTML.h"
// These classes only work with UIKit on iOS
#if TARGET_OS_IPHONE
@@ -37,7 +37,7 @@ extern NSString *DTStrikeOutAttribute;
extern NSString *DTBackgroundColorAttribute;
extern NSString *DTShadowsAttribute;
extern NSString *DTHorizontalRuleStyleAttribute;
-extern NSString *DTPaddingAttribute;
+extern NSString *DTTextBlocksAttribute;
// macros
@@ -34,4 +34,4 @@
NSString *DTBackgroundColorAttribute = @"DTBackgroundColor";
NSString *DTShadowsAttribute = @"DTShadows";
NSString *DTHorizontalRuleStyleAttribute = @"DTHorizontalRuleStyle";
-NSString *DTPaddingAttribute = @"DTPadding";
+NSString *DTTextBlocksAttribute = @"DTTextBlocks";
@@ -10,11 +10,14 @@
#import <CoreText/CoreText.h>
@class DTCoreTextLayoutLine;
+@class DTTextBlock;
// the value to use if the height is unknown
#define CGFLOAT_OPEN_HEIGHT 16777215.0f
+typedef void (^DTCoreTextLayoutFrameTextBlockHandler)(DTTextBlock *textBlock, CGRect frame, CGContextRef context, BOOL *shouldDrawDefaultBackground);
+
@class DTCoreTextLayouter;
@@ -104,6 +107,14 @@
- (void)drawInContext:(CGContextRef)context drawImages:(BOOL)drawImages;
+/**
+ Set a custom handler to be executed before text belonging to a text block is drawn.
+
+ @param handler A DTCoreTextLayoutFrameTextBlockHandler block.
+*/
+@property (nonatomic, copy) DTCoreTextLayoutFrameTextBlockHandler textBlockHandler;
+
+
/**
@name Working with Glyphs
*/
Oops, something went wrong.

0 comments on commit 8004a34

Please sign in to comment.