customToastVew.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // customToastVew.m
  3. // Private-X
  4. //
  5. // Created by xd h on 2024/7/18.
  6. //
  7. #import "customToastVew.h"
  8. @interface customToastVew ()
  9. @property(nonatomic,copy)NSString*curText;
  10. @property(nonatomic,strong)UILabel*textlabel;
  11. @end
  12. @implementation customToastVew
  13. static customToastVew * _instance;
  14. + (customToastVew*)makeText:(NSString*)text
  15. {
  16. [customToastVew shardInstance].curText = text;
  17. return [customToastVew shardInstance];
  18. }
  19. + (instancetype)shardInstance {
  20. static dispatch_once_t customToastVewOnceToken;
  21. dispatch_once(&customToastVewOnceToken, ^{
  22. _instance = [[self alloc] init];
  23. });
  24. return _instance;
  25. }
  26. - (id)initWithFrame:(CGRect)frame
  27. {
  28. self = [super initWithFrame:frame];
  29. if (self)
  30. {
  31. [self drawAnyView];
  32. }
  33. return self;
  34. }
  35. - (void)drawAnyView
  36. {
  37. CGFloat titleSize = 16.f;
  38. //UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, textSize.width + 20, textSize.height + 20)];
  39. _textlabel = [[UILabel alloc] init];
  40. _textlabel.backgroundColor = [UIColor clearColor];
  41. _textlabel.textAlignment = NSTextAlignmentCenter;
  42. _textlabel.textColor = [UIColor whiteColor];
  43. _textlabel.font = [UIFont systemFontOfSize:titleSize];
  44. _textlabel.numberOfLines = 0;
  45. self.backgroundColor = [UIColor hwColor:@"#000000" alpha:0.8];
  46. self.layer.cornerRadius = 8;
  47. self.layer.masksToBounds = YES;
  48. [self addSubview:_textlabel];
  49. }
  50. - (void)show
  51. {
  52. CGFloat titleSize = 16.f;
  53. CGSize textSize = [_curText boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width * 0.75, 100)
  54. options:(NSStringDrawingUsesLineFragmentOrigin)
  55. attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:titleSize], NSForegroundColorAttributeName:[UIColor whiteColor]}
  56. context:nil].size;
  57. _textlabel.frame = CGRectMake(10, 10, textSize.width + 20, textSize.height + 20);
  58. _textlabel.text = _curText;
  59. self.frame = CGRectMake((SCREEN_W - (textSize.width + 40))/2, (SCREEN_H - (textSize.height + 40))/2, textSize.width + 40, textSize.height + 40);
  60. [ksharedAppDelegate.window insertSubview:self atIndex:0];
  61. //[ksharedAppDelegate.mainTabBar.view insertSubview:self atIndex:0];
  62. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  63. [self hideFun];
  64. });
  65. }
  66. -(void)hideFun
  67. {
  68. [self removeFromSuperview];
  69. }
  70. @end