Skip to content
This repository has been archived by the owner on Feb 16, 2019. It is now read-only.

Commit

Permalink
Convert SSLoadingView to use UILabel as its model. Fixes #75
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes committed Oct 9, 2011
1 parent 5599010 commit b8b2d76
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 104 deletions.
38 changes: 4 additions & 34 deletions SSToolkit/SSLoadingView.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,13 @@
@interface SSLoadingView : UIView

/**
A view that indicates loading activity to the user. (read-only)
*/
@property (nonatomic, retain, readonly) UIActivityIndicatorView *activityIndicatorView;

/**
The text that is displayed to the user.
The default is "Loading..."
*/
@property (nonatomic, copy) NSString *text;

/**
The font of the `text`.
*/
@property (nonatomic, retain) UIFont *font;

/**
The color of the text.
The default value is `[UIColor darkGrayColor]`.
*/
@property (nonatomic, retain) UIColor *textColor;

/**
The color of the text shadow.
Set to `nil` to disable drawing the shadow. The default value is `[UIColor whiteColor]`.
The text label that is displayed to the user.
*/
@property (nonatomic, retain) UIColor *shadowColor;
@property (nonatomic, retain, readonly) UILabel *textLabel;

/**
The shadow offset (measured in points) for the `text`.
The `shadowColor` must be non-nil for this property to have any effect. The default offset size is (0, 1), which
indicates a shadow one point below the text. Text shadows are drawn with the specified offset and color and no
blurring.
A view that indicates loading activity to the user. (read-only)
*/
@property (nonatomic, assign) CGSize shadowOffset;
@property (nonatomic, retain, readonly) UIActivityIndicatorView *activityIndicatorView;

@end
90 changes: 20 additions & 70 deletions SSToolkit/SSLoadingView.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,14 @@ @implementation SSLoadingView

#pragma mark - Accessors

@synthesize textLabel = _textLabel;
@synthesize activityIndicatorView = _activityIndicatorView;

@synthesize text = _text;

- (void)setText:(NSString *)text {
[_text release];
_text = [text copy];

[self setNeedsDisplay];
}


@synthesize font = _font;

- (void)setFont:(UIFont *)font {
[_font release];
_font = [font retain];

[self setNeedsDisplay];
}


@synthesize textColor = _textColor;

- (void)setTextColor:(UIColor *)textColor {
[_textColor release];
_textColor = [textColor retain];

[self setNeedsDisplay];
}


@synthesize shadowColor = _shadowColor;

- (void)setShadowColor:(UIColor *)shadowColor {
[_shadowColor release];
_shadowColor = [shadowColor retain];

[self setNeedsDisplay];
}


@synthesize shadowOffset = _shadowOffset;

- (void)setShadowOffset:(CGSize)shadowOffset {
_shadowOffset = shadowOffset;

[self setNeedsDisplay];
}


#pragma mark - NSObject

- (void)dealloc {
[_font release];
[_text release];
[_textColor release];
[_shadowColor release];
[_textLabel release];
[_activityIndicatorView release];
[super dealloc];
}
Expand All @@ -90,18 +40,21 @@ - (id)initWithFrame:(CGRect)frame {
self.opaque = YES;
self.contentMode = UIViewContentModeRedraw;

// Setup label
_textLabel = [[UILabel alloc] initWithFrame:CGRectZero];

// Setup the indicator
_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_activityIndicatorView.hidesWhenStopped = NO;
[_activityIndicatorView startAnimating];
[self addSubview:_activityIndicatorView];

// Defaults
self.text = @"Loading...";
self.font = [UIFont systemFontOfSize:16.0f];
self.textColor = [UIColor darkGrayColor];
self.shadowColor = [UIColor whiteColor];
_shadowOffset = CGSizeMake(0.0f, 1.0f);
_textLabel.text = @"Loading...";
_textLabel.font = [UIFont systemFontOfSize:16.0f];
_textLabel.textColor = [UIColor darkGrayColor];
_textLabel.shadowColor = [UIColor whiteColor];
_textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
}
return self;
}
Expand All @@ -112,29 +65,26 @@ - (void)drawRect:(CGRect)rect {
CGRect frame = self.frame;

// Calculate sizes
CGSize maxSize = CGSizeMake(frame.size.width - (interiorPadding * 2.0f) - indicatorSize - indicatorRightMargin, indicatorSize);
CGSize textSize = [_text sizeWithFont:_font constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap];
CGSize maxSize = CGSizeMake(frame.size.width - (interiorPadding * 2.0f) - indicatorSize - indicatorRightMargin,
indicatorSize);

CGSize textSize = [_textLabel.text sizeWithFont:_textLabel.font constrainedToSize:maxSize
lineBreakMode:UILineBreakModeWordWrap];

// Calculate position
CGFloat totalWidth = textSize.width + indicatorSize + indicatorRightMargin;
NSInteger y = (NSInteger)((frame.size.height / 2.0f) - (indicatorSize / 2.0f));

// Position the indicator
_activityIndicatorView.frame = CGRectMake((NSInteger)((frame.size.width - totalWidth) / 2.0f), y, indicatorSize, indicatorSize);
_activityIndicatorView.frame = CGRectMake((NSInteger)((frame.size.width - totalWidth) / 2.0f), y, indicatorSize,
indicatorSize);

// Calculate text position
CGRect textRect = CGRectMake(_activityIndicatorView.frame.origin.x + indicatorSize + indicatorRightMargin, y, textSize.width, textSize.height);

// Draw shadow
if (_shadowColor) {
[_shadowColor set];
CGRect shadowRect = CGRectMake(textRect.origin.x + _shadowOffset.width, textRect.origin.y + _shadowOffset.height, textRect.size.width, textRect.size.height);
[_text drawInRect:shadowRect withFont:_font lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentLeft];
}
CGRect textRect = CGRectMake(_activityIndicatorView.frame.origin.x + indicatorSize + indicatorRightMargin, y,
textSize.width, textSize.height);

// Draw text
[_textColor set];
[_text drawInRect:textRect withFont:_font lineBreakMode:UILineBreakModeTailTruncation alignment:UITextAlignmentLeft];
[_textLabel drawTextInRect:textRect];
}

@end

0 comments on commit b8b2d76

Please sign in to comment.