Permalink
Browse files

Added ability to draw links normal or highlighted

  • Loading branch information...
odrobnik committed Mar 21, 2013
1 parent 80826ca commit c2c55c095ba9f852f42f3a91ce2ab0440f26bbb1
@@ -416,7 +416,7 @@ - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
options |= DTCoreTextLayoutFrameDrawingOmitAttachments;
}
- if (!_shouldDrawImages)
+ if (!_shouldDrawLinks)
{
options |= DTCoreTextLayoutFrameDrawingOmitLinks;
}
@@ -58,6 +58,7 @@ extern NSString * const DTWillFlushBlockCallBack;
extern NSString * const DTTextListsAttribute;
extern NSString * const DTAttachmentParagraphSpacingAttribute;
extern NSString * const DTLinkAttribute;
+extern NSString * const DTLinkHighlightColorAttribute;
extern NSString * const DTAnchorAttribute;
extern NSString * const DTGUIDAttribute;
extern NSString * const DTHeaderLevelAttribute;
@@ -16,6 +16,7 @@
NSString * const DTDefaultFontSize = @"DTDefaultFontSize";
NSString * const DTDefaultTextColor = @"DTDefaultTextColor";
NSString * const DTDefaultLinkColor = @"DTDefaultLinkColor";
+//NSString * const DTDefaultLinkHighlightColor = @"DTDefaultLinkHighlightColor";
NSString * const DTDefaultLinkDecoration = @"DTDefaultLinkDecoration";
NSString * const DTDefaultTextAlignment = @"DTDefaultTextAlignment";
NSString * const DTDefaultLineHeightMultiplier = @"DTDefaultLineHeightMultiplier";
@@ -31,6 +32,7 @@
NSString * const DTTextListsAttribute = @"DTTextLists";
NSString * const DTAttachmentParagraphSpacingAttribute = @"DTAttachmentParagraphSpacing";
NSString * const DTLinkAttribute = @"NSLinkAttributeName";
+NSString * const DTLinkHighlightColorAttribute = @"DTLinkHighlightColor";
NSString * const DTAnchorAttribute = @"DTAnchor";
NSString * const DTGUIDAttribute = @"DTGUID";
NSString * const DTHeaderLevelAttribute = @"DTHeaderLevel";
@@ -206,6 +206,9 @@ - (void)drawInContext:(CGContextRef)context
CGContextSetTextMatrix(context, textMatrix);
+ CGContextSetRGBFillColor(context, 1, 0, 0, 1);
+ CGContextSetBlendMode(context, kCGBlendModePlusLighter);
+
CTRunDraw(_run, context, CFRangeMake(0, 0));
// restore identity
@@ -243,26 +246,6 @@ - (void)drawDecorationInContext:(CGContextRef)context
// exclude trailing whitespace so that we don't underline too much
CGRect runStrokeBounds = CGRectIntersection(lineFrame, self.frame);
- // get text color or use black
- id color = [_attributes objectForKey:(id)kCTForegroundColorAttributeName];
-
-#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_5_1
- if (!color && ___useiOS6Attributes)
- {
- UIColor *uiColor = [_attributes objectForKey:NSForegroundColorAttributeName];
- color = (id)uiColor.CGColor;
- }
-#endif
-
- if (color)
- {
- CGContextSetStrokeColorWithColor(context, (__bridge CGColorRef)color);
- }
- else
- {
- CGContextSetGrayStrokeColor(context, 0, 1.0);
- }
-
NSInteger superscriptStyle = [[_attributes objectForKey:(id)kCTSuperscriptAttributeName] integerValue];
switch (superscriptStyle)
@@ -837,6 +837,50 @@ - (void)drawInContext:(CGContextRef)context drawImages:(BOOL)drawImages drawLink
[self drawInContext:context options:options];
}
+// sets the text foreground color based on the glyph run and drawing options
+- (void)_setForgroundColorInContext:(CGContextRef)context forGlyphRun:(DTCoreTextGlyphRun *)glyphRun options:(DTCoreTextLayoutFrameDrawingOptions)options
+{
+ id color = nil;
+
+ BOOL needsToSetFillColor = [[glyphRun.attributes objectForKey:(id)kCTForegroundColorFromContextAttributeName] boolValue];
+
+ if (glyphRun.isHyperlink)
+ {
+ if (options & DTCoreTextLayoutFrameDrawingDrawLinksHighlighted)
+ {
+ color = (id)[[glyphRun.attributes objectForKey:DTLinkHighlightColorAttribute] CGColor];
+ }
+ }
+
+ if (!color)
+ {
+ // get text color or use black
+ color = [glyphRun.attributes objectForKey:(id)kCTForegroundColorAttributeName];
+
+#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_5_1
+ if (!color && ___useiOS6Attributes)
+ {
+ DTColor *uiColor = [glyphRun.attributes objectForKey:NSForegroundColorAttributeName];
+ color = (id)uiColor.CGColor;
+ }
+#endif
+ }
+
+ // default color
+ if (!color)
+ {
+ color = (id)[DTColor blackColor].CGColor;
+ }
+
+ // set fill for text that uses kCTForegroundColorFromContextAttributeName
+ if (needsToSetFillColor)
+ {
+ CGContextSetFillColorWithColor(context, (__bridge CGColorRef)color);
+ }
+
+ // set stroke for lines
+ CGContextSetStrokeColorWithColor(context, (__bridge CGColorRef)color);
+}
- (void)drawInContext:(CGContextRef)context options:(DTCoreTextLayoutFrameDrawingOptions)options
{
@@ -1004,6 +1048,8 @@ - (void)drawInContext:(CGContextRef)context options:(DTCoreTextLayoutFrameDrawin
continue;
}
+ [self _setForgroundColorInContext:context forGlyphRun:oneRun options:options];
+
[oneRun drawDecorationInContext:context];
}
}
@@ -1132,6 +1178,8 @@ - (void)drawInContext:(CGContextRef)context options:(DTCoreTextLayoutFrameDrawin
}
else // no shadows
{
+ [self _setForgroundColorInContext:context forGlyphRun:oneRun options:options];
+
[oneRun drawInContext:context];
}
}
@@ -8,6 +8,7 @@
#import "DTCoreText.h"
#import "DTHTMLElement.h"
+#import "DTHTMLElementA.h"
#import "DTHTMLElementAttachment.h"
#import "DTHTMLElementBR.h"
#import "DTHTMLElementHR.h"
@@ -39,6 +40,7 @@ + (void)initialize
// lookup table so that we quickly get the correct class to instantiate for special tags
NSMutableDictionary *tmpDict = [[NSMutableDictionary alloc] init];
+ [tmpDict setObject:[DTHTMLElementA class] forKey:@"a"];
[tmpDict setObject:[DTHTMLElementBR class] forKey:@"br"];
[tmpDict setObject:[DTHTMLElementHR class] forKey:@"hr"];
[tmpDict setObject:[DTHTMLElementLI class] forKey:@"li"];
@@ -0,0 +1,21 @@
+//
+// DTHTMLElementA.h
+// DTCoreText
+//
+// Created by Oliver Drobnik on 21.03.13.
+// Copyright (c) 2013 Drobnik.com. All rights reserved.
+//
+
+#import "DTHTMLElement.h"
+
+/**
+ Specialized subclass of <DTHTMLElement> that represents a hyperlink.
+ */
+@interface DTHTMLElementA : DTHTMLElement
+
+/**
+ Foreground text color of the receiver when highlighted
+ */
+@property (nonatomic, strong) DTColor *highlightedTextColor;
+
+@end
@@ -0,0 +1,47 @@
+//
+// DTHTMLElementA.m
+// DTCoreText
+//
+// Created by Oliver Drobnik on 21.03.13.
+// Copyright (c) 2013 Drobnik.com. All rights reserved.
+//
+
+#import "DTHTMLElementA.h"
+
+@implementation DTHTMLElementA
+{
+ DTColor *_highlightedTextColor;
+}
+
+- (void)applyStyleDictionary:(NSDictionary *)styles
+{
+ [super applyStyleDictionary:styles];
+
+ // TODO: get the highlighted color from CSS
+ self.highlightedTextColor = [DTColor redColor];
+}
+
+- (NSAttributedString *)attributedString
+{
+ // super returns a mutable attributed string
+ NSMutableAttributedString *mutableAttributedString = (NSMutableAttributedString *)[super attributedString];
+
+ if (_highlightedTextColor)
+ {
+ NSRange range = NSMakeRange(0, [mutableAttributedString length]);
+
+ // this additional attribute keeps the highlight color
+ [mutableAttributedString addAttribute:DTLinkHighlightColorAttribute value:(id)_highlightedTextColor range:range];
+
+ // we need to set the text color via the graphics context
+ [mutableAttributedString addAttribute:(id)kCTForegroundColorFromContextAttributeName value:[NSNumber numberWithBool:YES] range:range];
+ }
+
+ return mutableAttributedString;
+}
+
+#pragma mark - Properties
+
+@synthesize highlightedTextColor = _highlightedTextColor;
+
+@end
@@ -35,6 +35,14 @@
A060479516A8B5770000CAA1 /* DTAttributedLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = A060479116A8B5770000CAA1 /* DTAttributedLabel.m */; };
A060479616A8B5770000CAA1 /* DTAttributedLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = A060479116A8B5770000CAA1 /* DTAttributedLabel.m */; };
A060479816A8B5770000CAA1 /* DTAttributedLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = A060479116A8B5770000CAA1 /* DTAttributedLabel.m */; };
+ A700289916FB220B00AFEDA3 /* DTHTMLElementA.h in Headers */ = {isa = PBXBuildFile; fileRef = A700289716FB220B00AFEDA3 /* DTHTMLElementA.h */; };
+ A700289A16FB220B00AFEDA3 /* DTHTMLElementA.h in Headers */ = {isa = PBXBuildFile; fileRef = A700289716FB220B00AFEDA3 /* DTHTMLElementA.h */; };
+ A700289B16FB220B00AFEDA3 /* DTHTMLElementA.h in Headers */ = {isa = PBXBuildFile; fileRef = A700289716FB220B00AFEDA3 /* DTHTMLElementA.h */; };
+ A700289C16FB220B00AFEDA3 /* DTHTMLElementA.h in Headers */ = {isa = PBXBuildFile; fileRef = A700289716FB220B00AFEDA3 /* DTHTMLElementA.h */; };
+ A700289D16FB220B00AFEDA3 /* DTHTMLElementA.m in Sources */ = {isa = PBXBuildFile; fileRef = A700289816FB220B00AFEDA3 /* DTHTMLElementA.m */; };
+ A700289E16FB220B00AFEDA3 /* DTHTMLElementA.m in Sources */ = {isa = PBXBuildFile; fileRef = A700289816FB220B00AFEDA3 /* DTHTMLElementA.m */; };
+ A700289F16FB220B00AFEDA3 /* DTHTMLElementA.m in Sources */ = {isa = PBXBuildFile; fileRef = A700289816FB220B00AFEDA3 /* DTHTMLElementA.m */; };
+ A70028A016FB220B00AFEDA3 /* DTHTMLElementA.m in Sources */ = {isa = PBXBuildFile; fileRef = A700289816FB220B00AFEDA3 /* DTHTMLElementA.m */; };
A704C93A13901FDB0045CFC6 /* ImageIO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A704C93913901FDB0045CFC6 /* ImageIO.framework */; };
A7081EA415036460002987F1 /* DTTextBlock.h in Headers */ = {isa = PBXBuildFile; fileRef = A7081EA11503645F002987F1 /* DTTextBlock.h */; settings = {ATTRIBUTES = (Public, ); }; };
A7081EA515036460002987F1 /* DTTextBlock.m in Sources */ = {isa = PBXBuildFile; fileRef = A7081EA215036460002987F1 /* DTTextBlock.m */; };
@@ -741,6 +749,8 @@
288765A40DF7441C002DB57D /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
A060479016A8B5770000CAA1 /* DTAttributedLabel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DTAttributedLabel.h; sourceTree = "<group>"; };
A060479116A8B5770000CAA1 /* DTAttributedLabel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DTAttributedLabel.m; sourceTree = "<group>"; };
+ A700289716FB220B00AFEDA3 /* DTHTMLElementA.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DTHTMLElementA.h; sourceTree = "<group>"; };
+ A700289816FB220B00AFEDA3 /* DTHTMLElementA.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DTHTMLElementA.m; sourceTree = "<group>"; };
A704C93913901FDB0045CFC6 /* ImageIO.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ImageIO.framework; path = System/Library/Frameworks/ImageIO.framework; sourceTree = SDKROOT; };
A7081EA11503645F002987F1 /* DTTextBlock.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DTTextBlock.h; sourceTree = "<group>"; };
A7081EA215036460002987F1 /* DTTextBlock.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DTTextBlock.m; sourceTree = "<group>"; };
@@ -1315,6 +1325,8 @@
A7949A4614CAF58C00A8CCDE /* DTHTMLAttributedStringBuilder.m */,
A788C92A14863E8700E1AFD9 /* DTHTMLElement.h */,
A788C92B14863E8700E1AFD9 /* DTHTMLElement.m */,
+ A700289716FB220B00AFEDA3 /* DTHTMLElementA.h */,
+ A700289816FB220B00AFEDA3 /* DTHTMLElementA.m */,
A7EB84B5168B948000D686B7 /* DTHTMLElementBR.h */,
A7EB84B6168B948000D686B7 /* DTHTMLElementBR.m */,
A7EB84D3168B9B1100D686B7 /* DTHTMLElementHR.h */,
@@ -1501,6 +1513,7 @@
A060479216A8B5770000CAA1 /* DTAttributedLabel.h in Headers */,
A70E62D616C24905009B47BF /* DTDictationPlaceholderTextAttachment.h in Headers */,
A70E62E016C24DDF009B47BF /* DTDictationPlaceholderView.h in Headers */,
+ A700289916FB220B00AFEDA3 /* DTHTMLElementA.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1558,6 +1571,7 @@
A060479316A8B5770000CAA1 /* DTAttributedLabel.h in Headers */,
A70E62D716C24905009B47BF /* DTDictationPlaceholderTextAttachment.h in Headers */,
A70E62E116C24DDF009B47BF /* DTDictationPlaceholderView.h in Headers */,
+ A700289A16FB220B00AFEDA3 /* DTHTMLElementA.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1614,6 +1628,7 @@
A060479416A8B5770000CAA1 /* DTAttributedLabel.h in Headers */,
A70E62D816C24905009B47BF /* DTDictationPlaceholderTextAttachment.h in Headers */,
A70E62E216C24DDF009B47BF /* DTDictationPlaceholderView.h in Headers */,
+ A700289B16FB220B00AFEDA3 /* DTHTMLElementA.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1650,6 +1665,7 @@
A7EB84D8168B9B1300D686B7 /* DTHTMLElementHR.h in Headers */,
A7EB84E8168C657100D686B7 /* DTHTMLElementLI.h in Headers */,
A70E62D916C24905009B47BF /* DTDictationPlaceholderTextAttachment.h in Headers */,
+ A700289C16FB220B00AFEDA3 /* DTHTMLElementA.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -2151,6 +2167,7 @@
A060479516A8B5770000CAA1 /* DTAttributedLabel.m in Sources */,
A70E62DA16C24905009B47BF /* DTDictationPlaceholderTextAttachment.m in Sources */,
A70E62E316C24DDF009B47BF /* DTDictationPlaceholderView.m in Sources */,
+ A700289D16FB220B00AFEDA3 /* DTHTMLElementA.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -2206,6 +2223,7 @@
A060479616A8B5770000CAA1 /* DTAttributedLabel.m in Sources */,
A70E62DB16C24905009B47BF /* DTDictationPlaceholderTextAttachment.m in Sources */,
A70E62E416C24DDF009B47BF /* DTDictationPlaceholderView.m in Sources */,
+ A700289E16FB220B00AFEDA3 /* DTHTMLElementA.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -2318,6 +2336,7 @@
A060479816A8B5770000CAA1 /* DTAttributedLabel.m in Sources */,
A70E62DC16C24905009B47BF /* DTDictationPlaceholderTextAttachment.m in Sources */,
A70E62E516C24DDF009B47BF /* DTDictationPlaceholderView.m in Sources */,
+ A700289F16FB220B00AFEDA3 /* DTHTMLElementA.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -2357,6 +2376,7 @@
A7EB84EE168C657100D686B7 /* DTHTMLElementLI.m in Sources */,
A70E62DD16C24905009B47BF /* DTDictationPlaceholderTextAttachment.m in Sources */,
A70E62FC16C2540C009B47BF /* DTHTMLElementStylesheet.m in Sources */,
+ A70028A016FB220B00AFEDA3 /* DTHTMLElementA.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

0 comments on commit c2c55c0

Please sign in to comment.