UIAlertController+TUICustomStyle.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // UIAlertController+TUICustomStyle.m
  3. // TUIChat
  4. //
  5. // Created by wyl on 2022/10/20.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import <TIMCommon/TIMDefine.h>
  9. #import <objc/runtime.h>
  10. #import "UIAlertController+TUICustomStyle.h"
  11. @implementation TUICustomActionSheetItem
  12. - (instancetype)initWithTitle:(NSString *)title leftMark:(UIImage *)leftMark withActionHandler:(void (^)(UIAlertAction *action))actionHandler {
  13. self = [super init];
  14. if (self) {
  15. _title = title;
  16. _leftMark = leftMark;
  17. _actionHandler = actionHandler;
  18. }
  19. return self;
  20. }
  21. @end
  22. CGFloat padding = 10;
  23. CGFloat itemHeight = 57;
  24. CGFloat lineHeight = 0.5;
  25. CGFloat itemCount = 2;
  26. @interface UIAlertController ()
  27. @end
  28. @implementation UIAlertController (TUICustomStyle)
  29. - (void)configItems:(NSArray<TUICustomActionSheetItem *> *)items {
  30. CGFloat alertVCWidth = self.view.frame.size.width - 2 * padding;
  31. if (items.count > 0) {
  32. for (int i = 0; i < items.count; i++) {
  33. UIView *itemView = [[UIView alloc] init];
  34. itemView.frame = CGRectMake(padding, (itemHeight + lineHeight) * i, alertVCWidth - 2 * padding, itemHeight);
  35. itemView.userInteractionEnabled = NO;
  36. [self.view addSubview:itemView];
  37. UIImageView *icon = [[UIImageView alloc] init];
  38. [itemView addSubview:icon];
  39. icon.contentMode = UIViewContentModeScaleAspectFit;
  40. icon.frame = CGRectMake(kScale390(20), itemHeight * 0.5 - kScale390(30) * 0.5, kScale390(30), kScale390(30));
  41. icon.image = items[i].leftMark;
  42. UILabel *l = [[UILabel alloc] init];
  43. l.frame = CGRectMake(icon.frame.origin.x + icon.frame.size.width + padding + kScale390(15), 0, alertVCWidth * 0.5, itemHeight);
  44. l.text = items[i].title;
  45. l.font = [UIFont systemFontOfSize:17];
  46. l.textAlignment = isRTL()? NSTextAlignmentRight:NSTextAlignmentLeft;
  47. l.textColor = [UIColor systemBlueColor];
  48. l.userInteractionEnabled = false;
  49. [itemView addSubview:l];
  50. if (isRTL()) {
  51. [icon resetFrameToFitRTL];
  52. [l resetFrameToFitRTL];
  53. }
  54. }
  55. }
  56. // actions
  57. if (items.count > 0) {
  58. for (int i = 0; i < items.count; i++) {
  59. UIAlertAction *action = [UIAlertAction actionWithTitle:@"" style:items[i].actionStyle handler:items[i].actionHandler];
  60. [self addAction:action];
  61. }
  62. }
  63. }
  64. @end