Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[BREAKING] removed custom string drawing from DTLinkButton

Warning: this breaks the previous implementation approach to set attributedString and highlightedAttributedString properties. New new method is to set two different images for normal and highlighted, see DemoTextViewController implementation.
  • Loading branch information...
commit f445d06ea44999b566e9a37c21d2f60ff826265d 1 parent 5f3ae76
@Cocoanetics authored
Showing with 1 addition and 255 deletions.
  1. +1 −11 Core/Source/DTLinkButton.h
  2. +0 −244 Core/Source/DTLinkButton.m
View
12 Core/Source/DTLinkButton.h
@@ -15,7 +15,7 @@ extern NSString *DTLinkButtonDidHighlightNotification;
/**
A button that corresponds to a hyperlink.
- Multiple parts of the same hyperlink synchronize their looks through the guid.
+ Multiple parts of the same hyperlink synchronize their looks through the guid. You can show link text in a different color for normal and highlighted mode by setting the button images for these states.
*/
@interface DTLinkButton : UIButton
@@ -43,15 +43,5 @@ extern NSString *DTLinkButtonDidHighlightNotification;
*/
@property(nonatomic) BOOL showsTouchWhenHighlighted;
-/**
- The attributed string to be drawn for the link button.
- */
-@property (nonatomic, retain) NSAttributedString *attributedString;
-
-
-/**
- The attributed string to be drawn for the link button while it is highlighted.
- */
-@property (nonatomic, retain) NSAttributedString *highlightedAttributedString;
@end
View
244 Core/Source/DTLinkButton.m
@@ -27,14 +27,6 @@ @implementation DTLinkButton
CGSize _minimumHitSize;
BOOL _showsTouchWhenHighlighted;
-
- // normal text
- NSAttributedString *_attributedString;
- DTCoreTextLayoutLine *_normalLine;
-
- // highlighted text
- NSAttributedString *_highlightedAttributedString;
- DTCoreTextLayoutLine *_highlightedLine;
}
- (id)initWithFrame:(CGRect)frame
@@ -58,233 +50,6 @@ - (id)initWithFrame:(CGRect)frame
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
-
-
-}
-
-#pragma mark Drawing the Link Text
-
-- (DTCoreTextLayoutLine *)_normalLine
-{
- if (!_normalLine && _attributedString)
- {
- DTCoreTextLayouter *layouter = [[DTCoreTextLayouter alloc] initWithAttributedString:_attributedString];
-
- CGRect infiniteRect = CGRectMake(0, 0, CGFLOAT_MAX, CGFLOAT_OPEN_HEIGHT);
- DTCoreTextLayoutFrame *frame = [[DTCoreTextLayoutFrame alloc] initWithFrame:infiniteRect layouter:layouter];
-
- if (![frame.lines count])
- {
- return nil;
- }
-
- // get the line
- _normalLine = [frame.lines objectAtIndex:0];
- }
-
- return _normalLine;
-}
-
-- (DTCoreTextLayoutLine *)_highlightedLine
-{
- if (!_highlightedLine && _highlightedAttributedString)
- {
- DTCoreTextLayouter *layouter = [[DTCoreTextLayouter alloc] initWithAttributedString:_highlightedAttributedString];
-
- CGRect infiniteRect = CGRectMake(0, 0, CGFLOAT_MAX, CGFLOAT_OPEN_HEIGHT);
- DTCoreTextLayoutFrame *frame = [[DTCoreTextLayoutFrame alloc] initWithFrame:infiniteRect layouter:layouter];
-
- if (![frame.lines count])
- {
- return nil;
- }
-
- // get the line
- _highlightedLine = [frame.lines objectAtIndex:0];
- }
-
- return _highlightedLine;
-}
-
-- (void)drawTextInContext:(CGContextRef)context highlighted:(BOOL)highlighted
-{
- DTCoreTextLayoutLine *lineToDraw = nil;
-
- if (highlighted)
- {
- // use highlighted glyph run
- lineToDraw = [self _highlightedLine];
- }
- else
- {
- // use normal glyph run
- lineToDraw = [self _normalLine];
- }
-
- if (!lineToDraw)
- {
- return;
- }
-
-
- for (DTCoreTextGlyphRun *glyphRunToDraw in lineToDraw.glyphRuns)
- {
- if ([glyphRunToDraw isTrailingWhitespace])
- {
- continue;
- }
-
- CGContextSaveGState(context);
-
- NSDictionary *runAttributes = glyphRunToDraw.attributes;
-
- // -------------- Line-Out, Underline, Background-Color
- BOOL drawStrikeOut = [[runAttributes objectForKey:DTStrikeOutAttribute] boolValue];
- BOOL drawUnderline = [[runAttributes objectForKey:(id)kCTUnderlineStyleAttributeName] boolValue];
-
- CGColorRef backgroundColor = (__bridge CGColorRef)[runAttributes objectForKey:DTBackgroundColorAttribute];
-
-#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_5_1
- if (!backgroundColor&&___useiOS6Attributes)
- {
- // could also be the iOS 6 background color
- DTColor *color = [runAttributes objectForKey:NSBackgroundColorAttributeName];
- backgroundColor = color.CGColor;
- }
-#endif
-
- if (drawStrikeOut||drawUnderline||backgroundColor)
- {
- // get text color or use black
- CGColorRef foregroundColor = (__bridge CGColorRef)[runAttributes objectForKey:(id)kCTForegroundColorAttributeName];
-
-#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_5_1
- // could also be an iOS 6 attribute
- if (!foregroundColor&&___useiOS6Attributes)
- {
- // could also be the iOS 6 background color
- DTColor *color = [runAttributes objectForKey:NSBackgroundColorAttributeName];
- foregroundColor = color.CGColor;
- }
-#endif
-
- if (foregroundColor)
- {
- CGContextSetStrokeColorWithColor(context, foregroundColor);
- }
- else
- {
- CGContextSetGrayStrokeColor(context, 0, 1.0);
- }
-
- CGRect runStrokeBounds = UIEdgeInsetsInsetRect(self.bounds, self.contentEdgeInsets);
- runStrokeBounds.origin.x = glyphRunToDraw.frame.origin.x + self.contentEdgeInsets.left;
- runStrokeBounds.size.width = glyphRunToDraw.frame.size.width;
-
- NSInteger superscriptStyle = [[glyphRunToDraw.attributes objectForKey:(id)kCTSuperscriptAttributeName] integerValue];
-
- switch (superscriptStyle)
- {
- case 1:
- {
- runStrokeBounds.origin.y -= glyphRunToDraw.ascent * 0.47f;
- break;
- }
- case -1:
- {
- runStrokeBounds.origin.y += glyphRunToDraw.ascent * 0.25f;
- break;
- }
- default:
- break;
- }
-
- if (backgroundColor)
- {
- CGContextSetFillColorWithColor(context, backgroundColor);
- CGContextFillRect(context, runStrokeBounds);
- }
-
- if (drawStrikeOut || drawUnderline)
- {
- CTFontRef usedFont = (__bridge CTFontRef)([glyphRunToDraw.attributes objectForKey:(id)kCTFontAttributeName]);
-
- if (usedFont)
- {
- CGFloat underlineThickness = CTFontGetUnderlineThickness(usedFont);
- CGContextSetLineWidth(context, underlineThickness);
- }
-
- if (drawStrikeOut)
- {
- CGFloat y;
-
- if (usedFont)
- {
- CGFloat strokePosition = CTFontGetXHeight(usedFont)/2.0;
- y = runStrokeBounds.origin.y + glyphRunToDraw.ascent - strokePosition;
- }
- else
- {
- y = roundf(runStrokeBounds.origin.y + glyphRunToDraw.frame.size.height/2.0f + 1)+0.5f;
- }
-
- CGContextMoveToPoint(context, runStrokeBounds.origin.x, y);
- CGContextAddLineToPoint(context, runStrokeBounds.origin.x + runStrokeBounds.size.width, y);
-
- CGContextStrokePath(context);
- }
-
- if (drawUnderline)
- {
- CGFloat y;
-
- if (usedFont)
- {
- CGFloat underlinePosition = CTFontGetUnderlinePosition(usedFont);
- y = runStrokeBounds.origin.y + runStrokeBounds.size.height - glyphRunToDraw.descent - underlinePosition;
- }
- else
- {
- y = roundf(runStrokeBounds.origin.y + runStrokeBounds.size.height - glyphRunToDraw.descent + 1)+0.5f;
- }
-
- CGContextMoveToPoint(context, runStrokeBounds.origin.x, y);
- CGContextAddLineToPoint(context, runStrokeBounds.origin.x + runStrokeBounds.size.width, y);
-
- CGContextStrokePath(context);
- }
- }
-
- // Flip the coordinate system
- CGContextSetTextMatrix(context, CGAffineTransformIdentity);
- CGContextScaleCTM(context, 1.0, -1.0);
- CGContextTranslateCTM(context, 0, -self.bounds.size.height);
-
- CGPoint textPosition = CGPointMake(self.contentEdgeInsets.left, ceilf(glyphRunToDraw.descent+self.contentEdgeInsets.bottom));
-
- switch (superscriptStyle)
- {
- case 1:
- {
- textPosition.y += glyphRunToDraw.ascent * 0.47f;
- break;
- }
- case -1:
- {
- textPosition.y -= glyphRunToDraw.ascent * 0.25f;
- break;
- }
- default:
- break;
- }
-
- CGContextSetTextPosition(context, textPosition.x, textPosition.y);
-
- [glyphRunToDraw drawInContext:context];
- CGContextRestoreGState(context);
- }
- }
}
#pragma mark Drawing the Run
@@ -295,8 +60,6 @@ - (void)drawRect:(CGRect)rect
if (self.highlighted)
{
- [self drawTextInContext:ctx highlighted:YES];
-
if (_showsTouchWhenHighlighted)
{
CGRect imageRect = [self contentRectForBounds:self.bounds];
@@ -306,10 +69,6 @@ - (void)drawRect:(CGRect)rect
[roundedPath fill];
}
}
- else
- {
- [self drawTextInContext:ctx highlighted:NO];
- }
}
#pragma mark Utilitiy
@@ -418,7 +177,4 @@ - (void)setMinimumHitSize:(CGSize)minimumHitSize
@synthesize minimumHitSize = _minimumHitSize;
@synthesize showsTouchWhenHighlighted = _showsTouchWhenHighlighted;
-@synthesize attributedString = _attributedString;
-@synthesize highlightedAttributedString = _highlightedAttributedString;
-
@end
Please sign in to comment.
Something went wrong with that request. Please try again.