YMCreateGreetingTemplateImageView.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // YMCreateGreetingTemplateImageView.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/2/25.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMCreateGreetingTemplateImageView.h"
  9. #import "YMCreateGreetingTemplateViewModel.h"
  10. @interface YMCreateGreetingTemplateImageView ()
  11. /// 创建打招呼模板VM
  12. @property (nonatomic, strong) YMCreateGreetingTemplateViewModel *viewModel;
  13. /// 提示标签
  14. @property (nonatomic, strong) UILabel *tipsLb;
  15. /// 模板图片视图
  16. @property (nonatomic, strong) UIImageView *templateImageView;
  17. /// 删除模板图片按钮
  18. @property (nonatomic, strong) UIButton *deleteTemplateImageBtn;
  19. @end
  20. @implementation YMCreateGreetingTemplateImageView
  21. - (void)ym_setupViews{
  22. [self addSubview:self.tipsLb];
  23. [self addSubview:self.templateImageView];
  24. [self setNeedsUpdateConstraints];
  25. [self updateConstraintsIfNeeded];
  26. }
  27. - (void)updateConstraints{
  28. [self.tipsLb mas_makeConstraints:^(MASConstraintMaker *make) {
  29. make.top.equalTo(self).offset(adapt(15));
  30. make.left.equalTo(self).offset(adapt(15));
  31. }];
  32. [self.templateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  33. make.top.equalTo(self.tipsLb.mas_bottom).offset(adapt(15));
  34. make.left.equalTo(self).offset(adapt(15));
  35. make.bottom.equalTo(self).offset(adapt(-15));
  36. make.width.height.mas_equalTo(adapt(100));
  37. }];
  38. [super updateConstraints];
  39. }
  40. - (void)ym_bindViewModel:(YMCreateGreetingTemplateViewModel *)viewModel{
  41. if (!viewModel) {
  42. return;
  43. }
  44. _viewModel = viewModel;
  45. @weakify(self)
  46. [[[[[RACObserve(self.viewModel, templateImageUrl) ignore:nil] distinctUntilChanged] deliverOnMainThread] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSString * templateImageUrl) {
  47. @strongify(self)
  48. [self.templateImageView sd_setImageWithURL:[LCTools getImageUrlWithAddress:templateImageUrl]];
  49. }];
  50. }
  51. - (UILabel *)tipsLb{
  52. if (!_tipsLb) {
  53. _tipsLb = [[UILabel alloc]init];
  54. NSString *tipsStr = @"添加图片(仅可添加一张)";
  55. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:tipsStr];
  56. attributedString.yy_font = LCFont(13);
  57. attributedString.yy_color = HexColorFromRGB(0x333333);
  58. attributedString.yy_alignment = NSTextAlignmentLeft;
  59. //设置高亮色和点击事件
  60. [attributedString yy_setColor:HexColorFromRGB(0x9d9d9d) range:[tipsStr rangeOfString:@"(仅可添加一张)"]];
  61. _tipsLb.attributedText = attributedString;
  62. }
  63. return _tipsLb;
  64. }
  65. - (UIImageView *)templateImageView{
  66. if (!_templateImageView) {
  67. _templateImageView = [[UIImageView alloc]init];
  68. _templateImageView.image = ImageByName(@"ym_edit_profile_add_icon");
  69. // _templateImageView.contentMode = UIViewContentModeScaleAspectFill;
  70. // _templateImageView.backgroundColor = HexColorFromRGB(0xF6F6F6);
  71. // _templateImageView.layer.cornerRadius = adapt(8);
  72. // _templateImageView.clipsToBounds = YES;
  73. _templateImageView.userInteractionEnabled = YES;
  74. WS(weakSelf)
  75. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
  76. [_templateImageView addGestureRecognizer:tap];
  77. [[[tap rac_gestureSignal] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  78. [weakSelf.viewModel openImagePickerPopupView];
  79. }];
  80. }
  81. return _templateImageView;
  82. }
  83. - (UIButton *)deleteTemplateImageBtn{
  84. if (!_deleteTemplateImageBtn) {
  85. _deleteTemplateImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  86. WS(weakSelf)
  87. [[[_deleteTemplateImageBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
  88. weakSelf.templateImageView.image = ImageByName(@"ym_edit_profile_add_icon");
  89. weakSelf.viewModel.templateImageUrl = @"";
  90. }];
  91. }
  92. return _deleteTemplateImageBtn;
  93. }
  94. @end