30#import "Toast+UIView.h"
31#import <QuartzCore/QuartzCore.h>
36#define kHorizontalPadding 10.0
37#define kVerticalPadding 10.0
38#define kCornerRadius 10.0
41#define kMaxTitleLines 999
42#define kMaxMessageLines 999
44#define kFadeDuration 0.2
46#define kDefaultLength 3
47#define kDefaultPosition @"bottom"
49#define kImageWidth 80.0
50#define kImageHeight 80.0
52@interface UIView (ToastPrivate)
54- (CGPoint)getPositionFor:(
id)position toast:(UIView *)toast;
55- (UIView *)makeViewForMessage:(NSString *)message title:(NSString *)title image:(UIImage *)image;
59@implementation UIView (Toast)
62#pragma mark Toast Methods
64- (void)makeToast:(NSString *)message
66 [
self makeToast:message duration:kDefaultLength position:kDefaultPosition];
69- (void)makeToast:(NSString *)message duration:(
float)interval position:(
id)point
71 UIView *toast = [
self makeViewForMessage:message title:nil image:nil];
72 [
self showToast:toast duration:interval position:point];
75- (void)makeToast:(NSString *)message
76 duration:(
float)interval
78 title:(NSString *)title
80 UIView *toast = [
self makeViewForMessage:message title:title image:nil];
81 [
self showToast:toast duration:interval position:point];
84- (void)makeToast:(NSString *)message
85 duration:(
float)interval
87 image:(UIImage *)image
89 UIView *toast = [
self makeViewForMessage:message title:nil image:image];
90 [
self showToast:toast duration:interval position:point];
93- (void)makeToast:(NSString *)message
94 duration:(
float)interval
96 title:(NSString *)title
97 image:(UIImage *)image
99 UIView *toast = [
self makeViewForMessage:message title:title image:image];
100 [
self showToast:toast duration:interval position:point];
103- (void)showToast:(UIView *)toast
105 [
self showToast:toast duration:kDefaultLength position:kDefaultPosition];
108- (void)showToast:(UIView *)toast duration:(
float)interval position:(
id)point
117 CGPoint toastPoint = [
self getPositionFor:point toast:toast];
119 [toast setCenter:toastPoint];
120 [toast setAlpha:0.0];
121 [
self addSubview:toast];
123 [UIView animateWithDuration:kFadeDuration
125 options:UIViewAnimationOptionCurveEaseOut
127 [toast setAlpha:1.0];
129 completion:^(BOOL finished) {
130 [UIView animateWithDuration:kFadeDuration
132 options:UIViewAnimationOptionCurveEaseIn
134 [toast setAlpha:0.0];
136 completion:^(BOOL finished) {
137 [toast removeFromSuperview];
143#pragma mark Private Methods
145- (CGPoint)getPositionFor:(
id)point toast:(UIView *)toast
155 if ([point isKindOfClass:[NSString class]])
158 if ([point caseInsensitiveCompare:
@"top"] == NSOrderedSame)
160 return CGPointMake(
self.bounds.size.width / 2,
161 (toast.frame.size.height / 2) + kVerticalPadding);
163 else if ([point caseInsensitiveCompare:
@"bottom"] == NSOrderedSame)
165 return CGPointMake(
self.bounds.size.width / 2,
166 (
self.bounds.size.height - (toast.frame.size.height / 2)) -
169 else if ([point caseInsensitiveCompare:
@"center"] == NSOrderedSame)
171 return CGPointMake(
self.bounds.size.width / 2,
self.bounds.size.height / 2);
174 else if ([point isKindOfClass:[NSValue class]])
176 return [point CGPointValue];
179 NSLog(
@"Error: Invalid position for toast.");
180 return [
self getPositionFor:kDefaultPosition toast:toast];
183- (UIView *)makeViewForMessage:(NSString *)message title:(NSString *)title image:(UIImage *)image
192 if ((message == nil) && (title == nil) && (image == nil))
195 UILabel *messageLabel = nil;
196 UILabel *titleLabel = nil;
197 UIImageView *imageView = nil;
200 UIView *wrapperView = [[[UIView alloc] init] autorelease];
201 [wrapperView.layer setCornerRadius:kCornerRadius];
202 [wrapperView setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:kOpacity]];
203 wrapperView.autoresizingMask =
204 UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin |
205 UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
209 imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
210 [imageView setContentMode:UIViewContentModeScaleAspectFit];
212 setFrame:CGRectMake(kHorizontalPadding, kVerticalPadding, kImageWidth, kImageHeight)];
215 float imageWidth, imageHeight, imageLeft;
218 if (imageView != nil)
220 imageWidth = imageView.bounds.size.width;
221 imageHeight = imageView.bounds.size.height;
222 imageLeft = kHorizontalPadding;
226 imageWidth = imageHeight = imageLeft = 0;
231 titleLabel = [[[UILabel alloc] init] autorelease];
232 [titleLabel setNumberOfLines:kMaxTitleLines];
233 [titleLabel setFont:[UIFont boldSystemFontOfSize:kFontSize]];
234 [titleLabel setTextAlignment:NSTextAlignmentLeft];
235 [titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
236 [titleLabel setTextColor:[UIColor whiteColor]];
237 [titleLabel setBackgroundColor:[UIColor clearColor]];
238 [titleLabel setAlpha:1.0];
239 [titleLabel setText:title];
242 CGSize maxSizeTitle = CGSizeMake((
self.bounds.size.width * kMaxWidth) - imageWidth,
243 self.bounds.size.height * kMaxHeight);
244 NSMutableParagraphStyle *titleStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
245 titleStyle.lineBreakMode = titleLabel.lineBreakMode;
246 CGRect titleRect = [title boundingRectWithSize:maxSizeTitle
247 options:NSStringDrawingUsesLineFragmentOrigin
249 NSFontAttributeName: titleLabel.font,
250 NSParagraphStyleAttributeName: titleStyle
253 CGSize expectedSizeTitle =
254 CGSizeMake(ceilf(titleRect.size.width), ceilf(titleRect.size.height));
255 [titleLabel setFrame:CGRectMake(0, 0, expectedSizeTitle.width, expectedSizeTitle.height)];
260 messageLabel = [[[UILabel alloc] init] autorelease];
261 [messageLabel setNumberOfLines:kMaxMessageLines];
262 [messageLabel setFont:[UIFont systemFontOfSize:kFontSize]];
263 [messageLabel setLineBreakMode:NSLineBreakByWordWrapping];
264 [messageLabel setTextColor:[UIColor whiteColor]];
265 [messageLabel setTextAlignment:NSTextAlignmentCenter];
266 [messageLabel setBackgroundColor:[UIColor clearColor]];
267 [messageLabel setAlpha:1.0];
268 [messageLabel setText:message];
271 CGSize maxSizeMessage = CGSizeMake((
self.bounds.size.width * kMaxWidth) - imageWidth,
272 self.bounds.size.height * kMaxHeight);
273 NSMutableParagraphStyle *messageStyle =
274 [[[NSMutableParagraphStyle alloc] init] autorelease];
275 messageStyle.lineBreakMode = messageLabel.lineBreakMode;
276 CGRect messageRect = [message boundingRectWithSize:maxSizeMessage
277 options:NSStringDrawingUsesLineFragmentOrigin
279 NSFontAttributeName: messageLabel.font,
280 NSParagraphStyleAttributeName: messageStyle
283 CGSize expectedSizeMessage =
284 CGSizeMake(ceilf(messageRect.size.width), ceilf(messageRect.size.height));
286 setFrame:CGRectMake(0, 0, expectedSizeMessage.width, expectedSizeMessage.height)];
290 float titleWidth, titleHeight, titleTop, titleLeft;
292 if (titleLabel != nil)
294 titleWidth = titleLabel.bounds.size.width;
295 titleHeight = titleLabel.bounds.size.height;
296 titleTop = kVerticalPadding;
297 titleLeft = imageLeft + imageWidth + kHorizontalPadding;
301 titleWidth = titleHeight = titleTop = titleLeft = 0;
305 float messageWidth, messageHeight, messageLeft, messageTop;
307 if (messageLabel != nil)
309 messageWidth = messageLabel.bounds.size.width;
310 messageHeight = messageLabel.bounds.size.height;
311 messageLeft = imageLeft + imageWidth + kHorizontalPadding;
312 messageTop = titleTop + titleHeight + kVerticalPadding;
316 messageWidth = messageHeight = messageLeft = messageTop = 0;
321 float longerWidth = (messageWidth < titleWidth) ? titleWidth : messageWidth;
322 float longerLeft = (messageLeft < titleLeft) ? titleLeft : messageLeft;
327 ((longerLeft + longerWidth + kHorizontalPadding) < imageWidth + (kHorizontalPadding * 2))
328 ? imageWidth + (kHorizontalPadding * 2)
329 : (longerLeft + longerWidth + kHorizontalPadding);
330 float wrapperHeight =
331 ((messageTop + messageHeight + kVerticalPadding) < imageHeight + (kVerticalPadding * 2))
332 ? imageHeight + (kVerticalPadding * 2)
333 : (messageTop + messageHeight + kVerticalPadding);
335 [wrapperView setFrame:CGRectMake(0, 0, wrapperWidth, wrapperHeight)];
337 if (titleLabel != nil)
339 [titleLabel setFrame:CGRectMake(titleLeft, titleTop, titleWidth, titleHeight)];
340 [wrapperView addSubview:titleLabel];
343 if (messageLabel != nil)
345 [messageLabel setFrame:CGRectMake(messageLeft, messageTop, messageWidth, messageHeight)];
346 [wrapperView addSubview:messageLabel];
349 if (imageView != nil)
351 [wrapperView addSubview:imageView];