Permalink
Browse files

added ability to customize refresh text and date format

  • Loading branch information...
1 parent 739176d commit a69c0159110d8e68001775db31356f7220b9ca5a @chrispix chrispix committed Jan 3, 2012
Showing with 55 additions and 10 deletions.
  1. +6 −0 PullToRefreshView.h
  2. +33 −10 PullToRefreshView.m
  3. +7 −0 PullToRefreshView/PullToRefreshView/ViewController.m
  4. +9 −0 README
View
@@ -48,6 +48,8 @@ typedef enum {
UILabel *statusLabel;
CALayer *arrowImage;
UIActivityIndicatorView *activityView;
+
+ NSDateFormatter *dateFormatter;
}
@property (nonatomic, retain) UIScrollView *scrollView;
@@ -58,6 +60,10 @@ typedef enum {
@property (nonatomic, retain) CALayer *arrowImage;
@property (nonatomic, retain) UIActivityIndicatorView *activityView;
@property (assign, readonly) BOOL isBottom;
+@property (nonatomic, copy) NSString *pullToRefreshText;
+@property (nonatomic, copy) NSString *releaseToRefreshText;
+@property (nonatomic, copy) NSString *loadingText;
+@property (readwrite, nonatomic, retain) NSDateFormatter *dateFormatter;
- (void)refreshLastUpdatedDate;
View
@@ -51,6 +51,7 @@ @implementation PullToRefreshView
@synthesize lastUpdatedLabel, statusLabel, arrowImage, activityView;
@synthesize timeout;
@synthesize isBottom;
+@synthesize pullToRefreshText, releaseToRefreshText, loadingText;
static const CGFloat kViewHeight = 60.0f;
static const CGFloat kScrollLimit = 65.0f;
@@ -151,39 +152,58 @@ - (id)initWithWebView:(UIWebView *)webView atBottom:(BOOL)atBottom {
#pragma mark -
#pragma mark Setters
+- (NSDateFormatter *)dateFormatter {
+ static NSDateFormatter *defaultFormatter;
+
+ if (dateFormatter) {
+ return dateFormatter;
+ } else if (!defaultFormatter) {
+ defaultFormatter = [[NSDateFormatter alloc] init];
+ [defaultFormatter setAMSymbol:@"AM"];
+ [defaultFormatter setPMSymbol:@"PM"];
+ [defaultFormatter setDateFormat:@"MM/dd/yy hh:mm a"];
+ }
+
+ return defaultFormatter;
+}
+
+- (void)setDateFormatter:(NSDateFormatter *)formatter {
+ if (dateFormatter != formatter) {
+ [dateFormatter release];
+ dateFormatter = [formatter retain];
+ [self refreshLastUpdatedDate];
+ }
+}
+
- (void)refreshLastUpdatedDate {
NSDate *date = [NSDate date];
if ([delegate respondsToSelector:@selector(pullToRefreshViewLastUpdated:)])
date = [delegate pullToRefreshViewLastUpdated:self];
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
- [formatter setAMSymbol:@"AM"];
- [formatter setPMSymbol:@"PM"];
- [formatter setDateFormat:@"MM/dd/yy hh:mm a"];
- self.lastUpdatedLabel.text = [NSString stringWithFormat:@"Last Updated: %@", [formatter stringFromDate:date]];
- [formatter release];
+ self.lastUpdatedLabel.text = [NSString stringWithFormat:@"Last Updated: %@", [[self dateFormatter] stringFromDate:date]];
}
- (void)setState:(PullToRefreshViewState)state_ {
state = state_;
switch (state) {
case PullToRefreshViewStateReady:
- self.statusLabel.text = @"Release to refresh...";
+ self.statusLabel.text = self.releaseToRefreshText ? self.releaseToRefreshText : @"Release to refresh...";
[self showActivity:NO animated:NO];
[self setImageFlipped:YES];
break;
case PullToRefreshViewStateNormal:
- self.statusLabel.text = [NSString stringWithFormat:@"Pull %@ to refresh...", isBottom ? @"up" : @"down"];
+ self.statusLabel.text = self.pullToRefreshText ? self.pullToRefreshText :
+ [NSString stringWithFormat:@"Pull %@ to refresh...", isBottom ? @"up" : @"down"];
[self showActivity:NO animated:NO];
[self setImageFlipped:NO];
[self refreshLastUpdatedDate];
break;
case PullToRefreshViewStateLoading:
- self.statusLabel.text = @"Loading...";
+ self.statusLabel.text = self.loadingText ? self.loadingText : @"Loading...";
[self showActivity:YES animated:YES];
[self setImageFlipped:NO];
[self parkVisible];
@@ -316,7 +336,10 @@ - (void)dealloc {
[lastUpdatedLabel release];
[timer invalidate];
[timer release];
-
+ [pullToRefreshText release];
+ [releaseToRefreshText release];
+ [loadingText release];
+ [dateFormatter release];
[super dealloc];
}
@@ -21,10 +21,17 @@ -(void)viewDidLoad {
self.topPull = [[PullToRefreshView alloc] initWithScrollView:self.scrollView];
[self.topPull setDelegate:self];
+ self.topPull.loadingText = @"Refreshing stories...";
+ NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
+ formatter.dateStyle = NSDateFormatterShortStyle;
+ formatter.timeStyle = NSDateFormatterNoStyle;
+ self.topPull.dateFormatter = formatter;
+
[self.scrollView addSubview:self.topPull];
self.bottomPull = [[PullToRefreshView alloc] initWithScrollView:self.scrollView atBottom:YES];
[self.bottomPull setDelegate:self];
+ self.bottomPull.loadingText = @"Loading more stories...";
[self.scrollView addSubview:self.bottomPull];
}
View
9 README
@@ -40,3 +40,12 @@ To customize the appearance, edit the statusLabel, lastUpdatedLabel, activityVie
pullToRefreshView.arrowImage.contents = (id) [UIImage imageNamed:@"my_arrow"].CGImage;
+To customize the content, edit the pullToRefreshText, releaseToRefreshText, loadingText, and dateFormatter properties:
+ pullToRefreshView.pullToRefreshText = @"Pull up to load more stories...";
+ pullToRefreshView.releaseToRefreshText = @"Release to refresh stories...";
+ pullToRefreshView.loadingText = @"Refreshing stories...";
+
+ NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
+ formatter.dateStyle = NSDateFormatterShortStyle;
+ formatter.timeStyle = NSDateFormatterNoStyle;
+ pullToRefreshView.dateFormatter = formatter;

0 comments on commit a69c015

Please sign in to comment.