Skip to content

Commit

Permalink
remove addTarget:onTapAction: from CLCGImageView to use the equivalen…
Browse files Browse the repository at this point in the history
…t method in the UIView's category
  • Loading branch information
ettore committed Sep 3, 2014
1 parent 452beb5 commit 88dcb25
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 40 deletions.
3 changes: 3 additions & 0 deletions CLCGCachedViewModel.h
Expand Up @@ -15,5 +15,8 @@
useAttributed:(BOOL)use_attributed
lineLimit:(NSUInteger)line_limit;

-(CGFloat)cachedHeightForAttributedString:(NSAttributedString*)attr_str
width:(CGFloat)w
lineLimit:(NSUInteger)line_limit;

@end
45 changes: 41 additions & 4 deletions CLCGCachedViewModel.m
Expand Up @@ -4,7 +4,6 @@
//
// Created by Pasquini, Ettore on 8/24/14.
//
//

#import "CLCGUIViewCategory.h"
#import "CLCGCachedViewModel.h"
Expand All @@ -29,6 +28,35 @@ -(id)init
}


-(CGFloat)cachedHeightForAttributedString:(NSAttributedString*)attr_str
width:(CGFloat)w
lineLimit:(NSUInteger)line_limit
{
NSString *key = [self hashKeyForAttributedString:attr_str
width:w
lineLimit:line_limit];
NSNumber *val = [_cache objectForKey:key];

if (val == nil && [attr_str length] > 0) {
CGSize text_size;
if (line_limit > 0){
NSAttributedString *s = [attr_str attributedSubstringFromRange:
NSMakeRange(0,1)];
CGFloat h = ceil([s sizeWithMaxW:w].height);
h *= line_limit;
text_size = [attr_str sizeWithMaxW:w maxH:h];
} else {
text_size = [attr_str sizeWithMaxW:w];
}

val = @(text_size.height);
[_cache setObject:val forKey:key];
}

return [val floatValue];
}


-(CGFloat)cachedTextHeightForSubview:(UIView*)view
width:(CGFloat)w
useAttributed:(BOOL)use_attributed
Expand Down Expand Up @@ -69,20 +97,29 @@ -(BOOL)shouldCacheForSubview:(UIView*)view
}


-(NSString*)hashKeyForAttributedString:(NSAttributedString*)attr_str
width:(CGFloat)w
lineLimit:(NSUInteger)line_limit
{
return [NSString stringWithFormat:@"%u-%.2f-%u", [attr_str hash], w, line_limit];
}


-(NSString*)hashKeyForSubview:(UIView*)view
width:(CGFloat)w
useAttributed:(BOOL)use_attributed
lineLimit:(NSUInteger)line_limit
{
NSString *text_hash;
if (use_attributed && [view respondsToSelector:@selector(attributedText)]) {
NSAttributedString *attr_text = [(id)view attributedText];
text_hash = [NSString stringWithFormat:@"%u", [attr_text hash]];
return [self hashKeyForAttributedString:[(id)view attributedText]
width:w
lineLimit:line_limit];
} else if ([view respondsToSelector:@selector(text)]
&& [view respondsToSelector:@selector(font)]) {
NSString *text = [(id)self text];
UIFont *font = [(id)self font];
text_hash = [NSString stringWithFormat:@"%u-%u", [text hash],[font hash]];
text_hash = [NSString stringWithFormat:@"%u-%u", [text hash], [font hash]];
} else {
text_hash = [NSString stringWithFormat:@"%u", [view hash]];
}
Expand Down
2 changes: 1 addition & 1 deletion CLCGCell.m
Expand Up @@ -151,7 +151,7 @@ -(UIImageView*)imageView
-(void)addTapActionOnImage:(void(^)())block;
{
self.tapActionBlock = block;
[_mainImageView addTarget:self onTapAction:@selector(tapAction:)];
[_mainImageView addTarget:self forTapAction:@selector(tapAction:)];
}


Expand Down
6 changes: 0 additions & 6 deletions CLCGImageView.h
Expand Up @@ -15,10 +15,4 @@ typedef void (^CLCGImageViewOnLoadCallback)(UIImage *img, int http_status);

-(void)loadImageForURL:(NSString*)url retinaURL:(NSString*)retinaurl;

/*!
* Adds an action to on touch-up event. The action signature is the standard:
* -(void)theAction
*/
-(void)addTarget:(id)target onTapAction:(SEL)action;

@end
30 changes: 2 additions & 28 deletions CLCGImageView.m
Expand Up @@ -6,6 +6,7 @@
// Copyright (c) 2012 Goodreads. All rights reserved.
//

// deprecated: we should really abstract this out of the class
#import "ASIHTTPRequest.h"

#import "clcg_macros.h"
Expand All @@ -14,22 +15,15 @@
#import "CLCGImageView.h"

@interface CLCGImageView ()
@property(nonatomic,strong) ASIHTTPRequest *req;
@property(nonatomic,strong) ASIHTTPRequest *req; //deprecated
@end

@implementation CLCGImageView
{
// target and action for "on tap" event
id _tapTarget;
SEL _tapAction;
}


-(void)dealloc
{
[_req clearDelegatesAndCancel];
_tapTarget = nil;
_tapAction = nil;
}


Expand All @@ -44,26 +38,6 @@ -(id)initWithFrame:(CGRect)frame
}


-(void)addTarget:(id)target onTapAction:(SEL)action
{
[self setUserInteractionEnabled:YES];

_tapAction = action;

// we want to keep an "assign" memory policy here to avoid circular references
// with container classes, who are likely to retain us. For instance, on a
// memory warning situation the container or vc will release us, and once
// viewDidLoad is re-hit, the client code will reassign the target in there.
_tapTarget = target;

UITapGestureRecognizer *tap_recognizer = [[UITapGestureRecognizer alloc]
initWithTarget:_tapTarget
action:_tapAction];
tap_recognizer.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap_recognizer];
}


-(void)loadImageForURL:(NSString*)normalurl retinaURL:(NSString*)retinaurl
{
if (_req) {
Expand Down
4 changes: 4 additions & 0 deletions categories/CLCGUIViewCategory.h
Expand Up @@ -210,6 +210,10 @@
below:(UIView*)vert_align_view vertPadding:(CGFloat)padding_vert
maxWidth:(CGFloat)max_w;

/*!
* Adds an action to be performed on a tap event occurring on the view.
* The action signature is a standard -(void)theAction.
*/
-(void)addTarget:(id)target forTapAction:(SEL)action;

@end
Expand Down
7 changes: 6 additions & 1 deletion categories/CLCGUIViewCategory.m
Expand Up @@ -307,6 +307,7 @@ -(void)addBorderWithInsets:(UIEdgeInsets)insets
[self sendSubviewToBack:border_line];
}


//------------------------------------------------------------------------------
#pragma mark - height measuring methods

Expand All @@ -321,6 +322,10 @@ -(CGFloat)textHeightForWidth:(CGFloat)w

if (use_attributed && [self respondsToSelector:@selector(attributedText)]) {
NSAttributedString *attr_text = [(id)self attributedText];

// TODO: instead of calculating these sizes each time, we should consider
// caching these somewhere

if ([attr_text length] > 0) {
if (line_limit > 0){
NSAttributedString *s = [attr_text attributedSubstringFromRange:
Expand Down Expand Up @@ -350,6 +355,7 @@ -(CGFloat)textHeightForWidth:(CGFloat)w
return text_size.height;
}


//------------------------------------------------------------------------------
#pragma mark - layout methods

Expand All @@ -364,7 +370,6 @@ -(void)putTextView:(UIView*)subview
const CGFloat x = round(CGRectGetMaxX(horiz_align_view.frame) + padding_horiz);
const CGFloat y = round(CGRectGetMaxY(vert_align_view.frame) + padding_vert);
const CGFloat w = max_w - x;

const CGFloat h = ceil([subview textHeightForWidth:w
useAttributed:use_attributed
lineLimit:line_limit]);
Expand Down

0 comments on commit 88dcb25

Please sign in to comment.