iToast.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. //
  13. //- (iToast *) settings;
  14. //
  15. //@end
  16. @implementation iToast
  17. - (id) initWithText:(NSString *) tex{
  18. if (self = [super init]) {
  19. text = [tex copy];
  20. }
  21. return self;
  22. }
  23. - (void) show{
  24. iToastSettings *theSettings = _settings;
  25. if (!theSettings) {
  26. theSettings = [iToastSettings getSharedSettings];
  27. }
  28. UIFont *font = [UIFont systemFontOfSize:14];
  29. CGSize textSize = [text boundingRectWithSize:CGSizeMake(280, 60) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size;
  30. textSize.height += 10;
  31. textSize.width += 10;
  32. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, textSize.width + 5, textSize.height + 5)];
  33. label.backgroundColor = [UIColor clearColor];
  34. label.textAlignment = NSTextAlignmentCenter;
  35. label.textColor = [UIColor whiteColor];
  36. label.font = font;
  37. label.text = text;
  38. label.alpha = 0 ;
  39. label.numberOfLines = 0;
  40. UIButton *v = [UIButton buttonWithType:UIButtonTypeCustom];
  41. v.frame = CGRectMake(0, 0, textSize.width + 200, textSize.height + 200);
  42. label.center = CGPointMake(v.frame.size.width / 2, v.frame.size.height / 2);
  43. [v addSubview:label];
  44. v.backgroundColor = [UIColor darkGrayColor]; // 背景不要透明
  45. v.layer.cornerRadius = 5;
  46. v.alpha = 0 ;
  47. UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
  48. CGPoint point = CGPointMake(window.frame.size.width/2, window.frame.size.height/2);
  49. if (theSettings.gravity == iToastGravityTop) {
  50. point = CGPointMake(window.frame.size.width / 2, 45);
  51. }else if (theSettings.gravity == iToastGravityBottom) {
  52. point = CGPointMake(window.frame.size.width / 2, window.frame.size.height - 45);
  53. }else if (theSettings.gravity == iToastGravityCenter) {
  54. point = CGPointMake(window.frame.size.width/2, window.frame.size.height/2);
  55. }else{
  56. point = theSettings.postition;
  57. }
  58. point = CGPointMake(point.x + offsetLeft, point.y + offsetTop);
  59. v.center = point;
  60. NSTimer *timer1 = [NSTimer timerWithTimeInterval:((float)theSettings.duration)/1000
  61. target:self selector:@selector(hideToast:)
  62. userInfo:nil repeats:NO];
  63. [[NSRunLoop mainRunLoop] addTimer:timer1 forMode:NSDefaultRunLoopMode];
  64. [window addSubview:v];
  65. [UIView beginAnimations:nil context:NULL];
  66. CGRect _frame= v.frame;
  67. float _w = textSize.width + 10;
  68. float _h = textSize.height + 10;
  69. float _x = _frame.origin.x + (_frame.size.width - _w)/2;
  70. float _y = _frame.origin.y + (_frame.size.height - _h)/2;
  71. [label setFrame:CGRectMake(2, 2, textSize.width + 5, textSize.height + 5)];
  72. label.alpha = 1 ;
  73. [v setFrame:CGRectMake(_x,_y,_w,_h)];
  74. v.alpha = 1;
  75. [UIView commitAnimations];
  76. view = v;
  77. [v addTarget:self action:@selector(hideToast:) forControlEvents:UIControlEventTouchDown];
  78. }
  79. - (void) removeToast{
  80. [view removeFromSuperview];
  81. }
  82. - (void) hideToast:(NSTimer*)theTimer{
  83. [UIView beginAnimations:nil context:NULL];
  84. view.alpha = 0;
  85. [UIView commitAnimations];
  86. [self removeToast];
  87. }
  88. + (iToast *) makeText:(NSString *) _text{
  89. iToast *toast = [[iToast alloc] initWithText:_text];
  90. return toast;
  91. }
  92. - (iToast *) setDuration:(NSInteger ) duration{
  93. [self theSettings].duration = duration;
  94. return self;
  95. }
  96. - (iToast *) setGravity:(iToastGravity) gravity
  97. offsetLeft:(NSInteger) left
  98. offsetTop:(NSInteger) top{
  99. [self theSettings].gravity = gravity;
  100. offsetLeft = left;
  101. offsetTop = top;
  102. return self;
  103. }
  104. - (iToast *) setGravity:(iToastGravity) gravity{
  105. [self theSettings].gravity = gravity;
  106. return self;
  107. }
  108. - (iToast *) setPostion:(CGPoint) _position{
  109. [self theSettings].postition = CGPointMake(_position.x, _position.y);
  110. return self;
  111. }
  112. -(iToastSettings *) theSettings{
  113. if (!_settings) {
  114. _settings = [[iToastSettings getSharedSettings] copy];
  115. }
  116. return _settings;
  117. }
  118. @end
  119. @implementation iToastSettings
  120. @synthesize duration;
  121. @synthesize gravity;
  122. @synthesize postition;
  123. @synthesize images;
  124. - (void) setImage:(UIImage *) img forType:(iToastType) type{
  125. if (!images) {
  126. images = [[NSMutableDictionary alloc] initWithCapacity:4];
  127. }
  128. if (img) {
  129. NSString *key = [NSString stringWithFormat:@"%i", type];
  130. [images setValue:img forKey:key];
  131. }
  132. }
  133. + (iToastSettings *) getSharedSettings{
  134. if (!sharedSettings) {
  135. sharedSettings = [iToastSettings new];
  136. sharedSettings.gravity = iToastGravityCenter;
  137. sharedSettings.duration = 1500; //增加显示时间,是能够截图
  138. }
  139. return sharedSettings;
  140. }
  141. - (id) copyWithZone:(NSZone *)zone{
  142. iToastSettings *copy = [iToastSettings new];
  143. copy.gravity = self.gravity;
  144. copy.duration = self.duration;
  145. copy.postition = self.postition;
  146. NSArray *keys = [self.images allKeys];
  147. for (NSString *key in keys){
  148. [copy setImage:[images valueForKey:key] forType:(iToastType)[key intValue]];
  149. }
  150. return copy;
  151. }
  152. @end