From c0ab9a6391720b88d027b5005fcbde6ff0114f78 Mon Sep 17 00:00:00 2001 From: "gtm.daemon" Date: Wed, 22 Feb 2012 17:30:10 +0000 Subject: [Author: noyau] This CL changes two things on the GTMFadeTruncatingLabel: * The class now respects the linebreak mode set on the superclass property (but still defaults to UILineBreakModeClip). * The API to create the fading mask is exposed as a class method so it can be reused outside in other contexts with the same display. R=thomasvl APPROVED=thomasvl DELTA=22 (13 added, 1 deleted, 8 changed) --- iPhone/GTMFadeTruncatingLabel.h | 8 +++++++- iPhone/GTMFadeTruncatingLabel.m | 22 ++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/iPhone/GTMFadeTruncatingLabel.h b/iPhone/GTMFadeTruncatingLabel.h index 5b768f0..5d9eb8c 100755 --- a/iPhone/GTMFadeTruncatingLabel.h +++ b/iPhone/GTMFadeTruncatingLabel.h @@ -1,7 +1,7 @@ // // GTMFadeTruncatingLabel.h // -// Copyright 2011 Google Inc. +// Copyright 2012 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy @@ -29,4 +29,10 @@ typedef enum { // Which side(s) to truncate. @property(nonatomic, assign) GTMFadeTruncatingMode truncateMode; +// Returns a linear gradient mask suitable to use with CGContextClipToMask() to +// fade the ends of a rectangle the same way this class does it. ++ (UIImage*)getLinearGradient:(CGRect)rect + fadeHead:(BOOL)fadeHead + fadeTail:(BOOL)fadeTail; + @end diff --git a/iPhone/GTMFadeTruncatingLabel.m b/iPhone/GTMFadeTruncatingLabel.m index 2a24a2c..440cc0e 100755 --- a/iPhone/GTMFadeTruncatingLabel.m +++ b/iPhone/GTMFadeTruncatingLabel.m @@ -1,7 +1,7 @@ // // GTMFadeTruncatingLabel.m // -// Copyright 2011 Google Inc. +// Copyright 2012 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy @@ -19,7 +19,6 @@ @interface GTMFadeTruncatingLabel () - (void)setup; -- (UIImage*)getLinearGradient:(CGRect)rect; @end @implementation GTMFadeTruncatingLabel @@ -34,6 +33,8 @@ - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { + // Use clip as a default value. + self.lineBreakMode = UILineBreakModeClip; [self setup]; } return self; @@ -50,7 +51,10 @@ CGSize size = [self.text sizeWithFont:self.font]; if (size.width > requestedRect.size.width) { - UIImage* image = [self getLinearGradient:requestedRect]; + UIImage* image = [[self class] + getLinearGradient:requestedRect + fadeHead:((self.truncateMode & GTMFadeTruncatingHead) > 0) + fadeTail:((self.truncateMode & GTMFadeTruncatingTail) > 0)]; CGContextClipToMask(context, self.bounds, image.CGImage); } @@ -60,21 +64,23 @@ self.shadowOffset.height); [self.text drawInRect:shadowRect withFont:self.font - lineBreakMode:UILineBreakModeClip + lineBreakMode:self.lineBreakMode alignment:self.textAlignment]; } CGContextSetFillColorWithColor(context, self.textColor.CGColor); [self.text drawInRect:requestedRect withFont:self.font - lineBreakMode:UILineBreakModeClip + lineBreakMode:self.lineBreakMode alignment:self.textAlignment]; CGContextRestoreGState(context); } // Create gradient opacity mask based on direction. -- (UIImage*)getLinearGradient:(CGRect)rect { ++ (UIImage*)getLinearGradient:(CGRect)rect + fadeHead:(BOOL)fadeHead + fadeTail:(BOOL)fadeTail { // Create an opaque context. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); CGContextRef context = CGBitmapContextCreate (NULL, @@ -100,13 +106,13 @@ CGFloat fadeWidth = MIN(rect.size.height * 2, floor(rect.size.width / 4)); CGFloat minX = CGRectGetMinX(rect); CGFloat maxX = CGRectGetMaxX(rect); - if (self.truncateMode & GTMFadeTruncatingTail) { + if (fadeTail) { CGFloat startX = maxX - fadeWidth; CGPoint startPoint = CGPointMake(startX, CGRectGetMidY(rect)); CGPoint endPoint = CGPointMake(maxX, CGRectGetMidY(rect)); CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); } - if (self.truncateMode & GTMFadeTruncatingHead) { + if (fadeHead) { CGFloat startX = minX + fadeWidth; CGPoint startPoint = CGPointMake(startX, CGRectGetMidY(rect)); CGPoint endPoint = CGPointMake(minX, CGRectGetMidY(rect)); -- cgit v1.2.3