QMAlert.m 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // QMAlert.m
  3. // IMSDK-OC
  4. //
  5. // Created by haochongfeng on 2017/5/17.
  6. // Copyright © 2017年 HCF. All rights reserved.
  7. //
  8. #import "QMAlert.h"
  9. @implementation QMAlert
  10. + (UIWindow *)mainWindow {
  11. UIApplication *app = [UIApplication sharedApplication];
  12. if ([app.delegate respondsToSelector:@selector(window)]) {
  13. return [app.delegate window];
  14. }
  15. else{
  16. return [app keyWindow];
  17. }
  18. }
  19. + (UIWindow *)lastWindow {
  20. UIApplication *app = [UIApplication sharedApplication];
  21. return app.windows.lastObject;
  22. }
  23. +(void)showMessage:(NSString *)message{
  24. UIWindow * window = [self mainWindow];
  25. UIView *showview = [[UIView alloc]init];
  26. showview.backgroundColor = [UIColor blackColor];
  27. showview.alpha = 0.8;
  28. showview.frame = CGRectMake(1, 1, 1, 1);
  29. showview.alpha = 1.0f;
  30. showview.layer.cornerRadius = 5.0f;
  31. showview.layer.masksToBounds = YES;
  32. [window addSubview:showview];
  33. UILabel *label = [[UILabel alloc]init];
  34. NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
  35. CGSize LabelSize = [message boundingRectWithSize:CGSizeMake(kScreenWidth - 100, 0) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
  36. label.frame = CGRectMake(10, 5, LabelSize.width, LabelSize.height);
  37. label.text = message;
  38. label.textColor = [UIColor whiteColor];
  39. label.textAlignment = 1;
  40. label.numberOfLines = 0;
  41. label.backgroundColor = [UIColor clearColor];
  42. label.font = [UIFont systemFontOfSize:14];
  43. [showview addSubview:label];
  44. showview.frame = CGRectMake((kScreenWidth - LabelSize.width - 20)/2, kScreenHeight - label.frame.size.height - 80, LabelSize.width + 20, LabelSize.height + 10);
  45. [UIView animateWithDuration:5 animations:^{
  46. showview.alpha = 0;
  47. } completion:^(BOOL finished) {
  48. [showview removeFromSuperview];
  49. }];
  50. }
  51. //计算文字高度
  52. + (CGFloat)calculateRowHeight:(NSString *)string fontSize:(NSInteger)fontSize{
  53. NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};//指定字号
  54. CGRect rect = [string boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 180, 0)/*计算高度要先指定宽度*/ options:NSStringDrawingUsesLineFragmentOrigin |
  55. NSStringDrawingUsesFontLeading attributes:dic context:nil];
  56. return rect.size.height;
  57. }
  58. @end