BDFaceToastView.m 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //
  2. // BDFaceToastView.m
  3. // FaceSDKSample_IOS
  4. //
  5. // Created by Zhang,Jian(MBD) on 2020/12/7.
  6. // Copyright © 2020 Baidu. All rights reserved.
  7. //
  8. #import "BDFaceToastView.h"
  9. @implementation BDFaceToastView
  10. + (void)showToast:(UIView *)superview text:(NSString *)text {
  11. CGFloat marginTotal = 90.0f;
  12. CGFloat textSize = 15.0f;
  13. UIColor *bgColor = [UIColor blackColor];
  14. CGFloat alpha = 0.8f;
  15. NSTimeInterval delayTime = 2;
  16. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(superview.frame) - marginTotal, 0)];
  17. label.font = [UIFont systemFontOfSize:textSize];
  18. label.text = text;
  19. label.numberOfLines = 0;
  20. label.textColor = [UIColor whiteColor];
  21. label.textAlignment = NSTextAlignmentCenter;
  22. [label sizeToFit];
  23. [label setNeedsLayout];
  24. CGRect labelRect = label.frame;
  25. labelRect.origin.x = textSize;
  26. labelRect.origin.y = textSize;
  27. label.frame = labelRect;
  28. CGRect bgRect = label.bounds;
  29. bgRect.size.width = bgRect.size.width + textSize * 2;
  30. bgRect.size.height = bgRect.size.height + textSize * 2;
  31. bgRect.origin.x = (CGRectGetWidth(superview.frame) - CGRectGetWidth(bgRect)) / 2.0f;
  32. bgRect.origin.y = (CGRectGetHeight(superview.frame) - CGRectGetHeight(bgRect)) / 2.0f;
  33. UIView *bgView = [[UIView alloc]initWithFrame:bgRect];
  34. UIView *contentBgView = [[UIView alloc] initWithFrame:bgView.bounds];
  35. [bgView addSubview:contentBgView];
  36. contentBgView.backgroundColor = bgColor;
  37. contentBgView.alpha = alpha;
  38. [bgView addSubview:label];
  39. bgView.layer.cornerRadius = CGRectGetHeight(bgView.frame) / 2.0f;
  40. bgView.clipsToBounds = YES;
  41. [superview addSubview:bgView];
  42. [self performSelector:@selector(removeMe:) withObject:bgView afterDelay:delayTime];
  43. }
  44. + (void)removeMe:(UIView *)view {
  45. if (view) {
  46. [view removeFromSuperview];
  47. view = nil;
  48. }
  49. }
  50. @end