JKToast.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // JKToast.m
  3. // JKToast
  4. //
  5. // Created by Jakey on 14-10-27.
  6. // Copyright (c) 2014年 www.skyfox.org. All rights reserved.
  7. //
  8. #import "JKToast.h"
  9. #import <QuartzCore/QuartzCore.h>
  10. @implementation JKToast
  11. - (id)initWithText:(NSString *)text{
  12. if (self = [super init]) {
  13. text = [text copy];
  14. UIFont *font = [UIFont systemFontOfSize:14];
  15. // CGSize textSize = [text sizeWithFont:font
  16. // constrainedToSize:CGSizeMake(280, MAXFLOAT)
  17. // lineBreakMode:NSLineBreakByCharWrapping];
  18. CGSize textSize = [text boundingRectWithSize:CGSizeMake(280, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size;
  19. CGRect textFrame = CGRectMake(0, 0, textSize.width + 40, textSize.height + 30);
  20. if(textLab){
  21. textLab.frame = textFrame;
  22. }else{
  23. textLab = [[UILabel alloc] initWithFrame:textFrame];
  24. textLab.backgroundColor = [UIColor clearColor];
  25. textLab.textColor = [UIColor darkTextColor];
  26. textLab.textAlignment = NSTextAlignmentCenter;
  27. textLab.numberOfLines = 0;
  28. textLab.font = font;
  29. }
  30. textLab.text = text;
  31. if (contentView) {
  32. contentView.frame = textFrame;
  33. }else{
  34. contentView = [[UIButton alloc] initWithFrame:textFrame];
  35. contentView.alpha = 0.0f;
  36. contentView.layer.cornerRadius = 23.0f;
  37. contentView.backgroundColor = RDSLightGrayColor;
  38. contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  39. [contentView addSubview:textLab];
  40. [contentView addTarget:self
  41. action:@selector(toastTaped:)
  42. forControlEvents:UIControlEventTouchDown];
  43. duration = DEFAULT_DISPLAY_DURATION;
  44. }
  45. // [[NSNotificationCenter defaultCenter] addObserver:self
  46. // selector:@selector(deviceOrientationDidChanged:)
  47. // name:UIDeviceOrientationDidChangeNotification
  48. // object:[UIDevice currentDevice]];
  49. }
  50. return self;
  51. }
  52. - (void)deviceOrientationDidChanged:(NSNotification *)notify{
  53. UIDevice* device = [notify valueForKey:@"object"];
  54. //[self hideAnimation];
  55. }
  56. -(void)toastTaped:(UIButton *)sender{
  57. [self hideAnimation];
  58. }
  59. - (void)setDuration:(CGFloat) duration{
  60. duration = duration;
  61. }
  62. -(void)showAnimation{
  63. [UIView animateWithDuration:0.3 animations:^{
  64. self->contentView.alpha = 1.0f;
  65. } completion:^(BOOL finished) {
  66. }];
  67. }
  68. -(void)hideAnimation{
  69. [UIView animateWithDuration:0.3 animations:^{
  70. self->contentView.alpha = 0.0f;
  71. } completion:^(BOOL finished) {
  72. }];
  73. }
  74. - (void)showWithLocal:(iToastLocal)local{
  75. // 取消延时任务
  76. dispatch_async(dispatch_get_main_queue(), ^{
  77. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideAnimation) object:nil];
  78. });
  79. UIWindow *window = [UIApplication sharedApplication].delegate.window;
  80. [window endEditing:YES];
  81. CGFloat centerY;
  82. if(local == localCenter){
  83. centerY = window.center.y;
  84. }else{
  85. centerY = window.bounds.size.height -160;
  86. }
  87. CGPoint center = CGPointMake(window.center.x, centerY);
  88. contentView.center = center;
  89. [window addSubview:contentView];
  90. [self showAnimation];
  91. dispatch_async(dispatch_get_main_queue(), ^{
  92. [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:self->duration];
  93. });
  94. }
  95. - (void)showFromTopOffset:(CGFloat) top{
  96. UIWindow *window = [UIApplication sharedApplication].delegate.window;
  97. contentView.center = CGPointMake(window.center.x, top + contentView.frame.size.height/2);
  98. [window addSubview:contentView];
  99. [self showAnimation];
  100. [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
  101. }
  102. - (void)showFromBottomOffset:(CGFloat) bottom{
  103. UIWindow *window = [UIApplication sharedApplication].delegate.window;
  104. contentView.center = CGPointMake(window.center.x, window.frame.size.height-(bottom + contentView.frame.size.height/2));
  105. [window addSubview:contentView];
  106. [self showAnimation];
  107. [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
  108. }
  109. + (JKToast*)sharedView {
  110. static dispatch_once_t once;
  111. static JKToast *sharedView;
  112. dispatch_once(&once, ^{ sharedView = [[self alloc] init]; });
  113. return sharedView;
  114. }
  115. + (void)showTopWithText:(NSString *)text{
  116. [JKToast showWithText:text local:localTop duration:3];
  117. }
  118. + (void)showBottomWithText:(NSString *)text{
  119. [JKToast showWithText:text local:localBottom duration:3];
  120. }
  121. + (void)showCenterWithText:(NSString *)text{
  122. [JKToast showWithText:text local:localCenter duration:3];
  123. }
  124. + (void)showWithText:(NSString *)text
  125. local:(iToastLocal)local
  126. duration:(CGFloat)duration{
  127. JKToast *toast = [[self sharedView] initWithText:text];
  128. [toast setDuration:duration];
  129. [toast showWithLocal:local];
  130. }
  131. + (void)showWithText:(NSString *)text
  132. topOffset:(CGFloat)topOffset{
  133. [JKToast showWithText:text topOffset:topOffset duration:DEFAULT_DISPLAY_DURATION];
  134. }
  135. + (void)showWithText:(NSString *)text
  136. topOffset:(CGFloat)topOffset
  137. duration:(CGFloat)duration{
  138. JKToast *toast = [[JKToast alloc] initWithText:text];
  139. [toast setDuration:duration];
  140. [toast showFromTopOffset:topOffset];
  141. }
  142. + (void)showWithText:(NSString *)text
  143. bottomOffset:(CGFloat)bottomOffset{
  144. [JKToast showWithText:text bottomOffset:bottomOffset duration:DEFAULT_DISPLAY_DURATION];
  145. }
  146. + (void)showWithText:(NSString *)text
  147. bottomOffset:(CGFloat)bottomOffset
  148. duration:(CGFloat)duration{
  149. JKToast *toast = [[JKToast alloc] initWithText:text] ;
  150. [toast setDuration:duration];
  151. [toast showFromBottomOffset:bottomOffset];
  152. }
  153. @end