iToast.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // iToast.m
  3. // iToast
  4. //
  5. // Created by Diallo Mamadou Bobo on 2/10/11.
  6. // Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "iToast.h"
  9. #import <QuartzCore/QuartzCore.h>
  10. static iToastSettings *sharedSettings = nil;
  11. @interface iToast(private)
  12. - (iToast *) settings;
  13. @end
  14. @implementation iToast
  15. - (id)initWithText:(NSString *)_text {
  16. if (self = [super init]) {
  17. text = [_text copy];
  18. }
  19. useAttr = NO;
  20. return self;
  21. }
  22. - (id)initWithAttrText:(NSAttributedString *)_text {
  23. if (self = [super init]) {
  24. attartext = [_text copy];
  25. }
  26. useAttr = YES;
  27. return self;
  28. }
  29. - (void)show {
  30. iToastSettings *theSettings = _settings;
  31. if (!theSettings) {
  32. theSettings = [iToastSettings getSharedSettings];
  33. }
  34. if (useAttr)
  35. {
  36. text = [attartext string];
  37. }
  38. CGFloat titleSize = 16.f;
  39. CGSize textSize = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width * 0.75, 100)
  40. options:(NSStringDrawingUsesLineFragmentOrigin)
  41. attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:titleSize], NSForegroundColorAttributeName:[UIColor whiteColor]}
  42. context:nil].size;
  43. //UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, textSize.width + 20, textSize.height + 20)];
  44. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, textSize.width + 2.f, (textSize.height + 20) > 50 ? (textSize.height + 20) : 50)];
  45. label.backgroundColor = [UIColor clearColor];
  46. label.textAlignment = NSTextAlignmentCenter;
  47. label.textColor = [UIColor whiteColor];
  48. label.font = [UIFont systemFontOfSize:titleSize];
  49. if ((useAttr))
  50. {
  51. [label setAttributedText:attartext];
  52. }
  53. else
  54. {
  55. label.text = text;
  56. }
  57. label.numberOfLines = 0;
  58. label.shadowColor = [UIColor darkGrayColor];
  59. label.shadowOffset = CGSizeMake(1, 1);
  60. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  61. button.frame = CGRectMake(0, 0, textSize.width + 20 + 2.f, (textSize.height + 20) > 50 ? (textSize.height + 20) : 50);
  62. label.center = CGPointMake(button.frame.size.width / 2, button.frame.size.height / 2);
  63. [button addSubview:label];
  64. [label release];
  65. button.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
  66. // button.backgroundColor = HWHUDColor;
  67. button.layer.cornerRadius = 5;
  68. button.center = CGPointMake([self getScreenSize].width/2, [self getScreenSize].height/2);
  69. NSTimer *timer = [NSTimer timerWithTimeInterval:((float)theSettings.duration)/1000 target:self selector:@selector(hideToast:) userInfo:nil repeats:NO];
  70. [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
  71. UIViewController *vc = [self appRootViewController];
  72. if ([vc isKindOfClass:[UIAlertController class]])
  73. {
  74. button.center = CGPointMake(vc.view.frame.size.width/2, vc.view.frame.size.height/2);
  75. }
  76. [vc.view addSubview:button];
  77. view = [button retain];
  78. [button addTarget:self action:@selector(hideToast:) forControlEvents:UIControlEventTouchDown];
  79. }
  80. - (void)hideToast:(NSTimer *)theTimer {
  81. [UIView beginAnimations:nil context:NULL];
  82. [view removeFromSuperview];
  83. [UIView commitAnimations];
  84. NSTimer *timer = [NSTimer timerWithTimeInterval:500 target:self selector:@selector(hideToast:) userInfo:nil repeats:NO];
  85. [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
  86. }
  87. - (void)removeToast:(NSTimer *)theTimer {
  88. [view removeFromSuperview];
  89. }
  90. + (iToast *)makeText:(NSString *)_text {
  91. iToast *toast = [[[iToast alloc] initWithText:_text] autorelease];
  92. return toast;
  93. }
  94. + (iToast *)makeAttrText:(NSAttributedString *)_attrtext {
  95. iToast *toast = [[[iToast alloc] initWithAttrText:_attrtext] autorelease];
  96. return toast;
  97. }
  98. - (iToast *)setDuration:(NSInteger)duration {
  99. [self theSettings].duration = duration;
  100. return self;
  101. }
  102. - (iToast *)setGravity:(iToastGravity)gravity offsetLeft:(NSInteger) left offsetTop:(NSInteger)top {
  103. [self theSettings].gravity = gravity;
  104. offsetLeft = left;
  105. offsetTop = top;
  106. return self;
  107. }
  108. - (iToast *)setGravity:(iToastGravity)gravity {
  109. [self theSettings].gravity = gravity;
  110. return self;
  111. }
  112. - (iToast *)setPostion:(CGPoint)_position {
  113. [self theSettings].postition = CGPointMake(_position.x, _position.y);
  114. return self;
  115. }
  116. - (iToastSettings *)theSettings {
  117. if (!_settings) {
  118. _settings = [[iToastSettings getSharedSettings] copy];
  119. }
  120. return _settings;
  121. }
  122. - (CGSize)getScreenSize {
  123. CGSize screenSize = [UIScreen mainScreen].bounds.size;
  124. if ((NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
  125. return CGSizeMake(screenSize.height, screenSize.width);
  126. }
  127. return screenSize;
  128. }
  129. - (UIViewController *)appRootViewController {
  130. UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
  131. UIViewController *topVC = appRootVC;
  132. while (topVC.presentedViewController) {
  133. topVC = topVC.presentedViewController;
  134. }
  135. return topVC;
  136. }
  137. @end
  138. @implementation iToastSettings
  139. @synthesize duration;
  140. @synthesize gravity;
  141. @synthesize postition;
  142. @synthesize images;
  143. - (void)setImage:(UIImage *)img forType:(iToastType)type{
  144. if (!images) {
  145. images = [[NSMutableDictionary alloc] initWithCapacity:4];
  146. }
  147. if (img) {
  148. NSString *key = [NSString stringWithFormat:@"%i", type];
  149. [images setValue:img forKey:key];
  150. }
  151. }
  152. + (iToastSettings *)getSharedSettings{
  153. if (!sharedSettings) {
  154. sharedSettings = [iToastSettings new];
  155. sharedSettings.gravity = iToastGravityCenter;
  156. sharedSettings.duration = iToastDurationNormal;
  157. }
  158. return sharedSettings;
  159. }
  160. - (id)copyWithZone:(NSZone *)zone{
  161. iToastSettings *copy = [iToastSettings new];
  162. copy.gravity = self.gravity;
  163. copy.duration = self.duration;
  164. copy.postition = self.postition;
  165. NSArray *keys = [self.images allKeys];
  166. for (NSString *key in keys){
  167. [copy setImage:[images valueForKey:key] forType:[key intValue]];
  168. }
  169. return copy;
  170. }
  171. @end