XYVoiceMatchButton.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // XYVoiceMatchButton.m
  3. // Starbuds
  4. //
  5. // Created by 翟玉磊 on 2020/12/4.
  6. // Copyright © 2020 翟玉磊. All rights reserved.
  7. //
  8. #import "XYVoiceMatchButton.h"
  9. @interface XYVoiceMatchButton ()
  10. @property (nonatomic, strong) UILabel *titleLabel;
  11. @property (nonatomic, strong) UILabel *subtitleLabel;
  12. @property (nonatomic, strong) UIButton *actionButton;
  13. @end
  14. @implementation XYVoiceMatchButton
  15. - (void)setTitle:(NSString *)title {
  16. _title = title;
  17. self.titleLabel.text = title;
  18. }
  19. - (void)setSubtitle:(NSString *)subtitle {
  20. _subtitle = subtitle;
  21. self.subtitleLabel.text = subtitle;
  22. if (StringIsEmpty(subtitle)) {
  23. // 只有一个标题
  24. [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  25. make.top.equalTo(self).offset(10.0f);
  26. }];
  27. }else {
  28. // 两个标题
  29. [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  30. make.top.equalTo(self).offset(3.0f);
  31. }];
  32. }
  33. }
  34. #pragma mark — Action
  35. - (void)actionButtonAction:(id)sender {
  36. if (self.actionButtonBlock) {
  37. self.actionButtonBlock();
  38. }
  39. }
  40. - (instancetype)init
  41. {
  42. self = [super init];
  43. if (self) {
  44. [self setupUI];
  45. }
  46. return self;
  47. }
  48. - (void)setupUI {
  49. [self addSubview:self.titleLabel];
  50. [self addSubview:self.subtitleLabel];
  51. [self addSubview:self.actionButton];
  52. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  53. make.left.equalTo(self).offset(10.0f);
  54. make.right.equalTo(self).offset(-10.0f);
  55. make.top.equalTo(self).offset(3.0f);
  56. make.height.equalTo(@20.0f);
  57. }];
  58. [self.subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.left.equalTo(self).offset(10.0f);
  60. make.right.equalTo(self).offset(-10.0f);
  61. make.top.equalTo(self.titleLabel.mas_bottom);
  62. make.height.equalTo(@14.0f);
  63. }];
  64. [self.actionButton mas_makeConstraints:^(MASConstraintMaker *make) {
  65. make.edges.equalTo(self);
  66. }];
  67. }
  68. #pragma mark — Getter
  69. - (UILabel *)titleLabel {
  70. if (!_titleLabel) {
  71. _titleLabel = [UILabel createLabelTextColor:Color_White fount:Font_B(14)];
  72. _titleLabel.textAlignment = NSTextAlignmentCenter;
  73. }
  74. return _titleLabel;
  75. }
  76. - (UILabel *)subtitleLabel {
  77. if (!_subtitleLabel) {
  78. _subtitleLabel = [UILabel createLabelTextColor:Color_White fount:Font(10)];
  79. _subtitleLabel.textAlignment = NSTextAlignmentCenter;
  80. }
  81. return _subtitleLabel;
  82. }
  83. - (UIButton *)actionButton {
  84. if (!_actionButton) {
  85. _actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
  86. _actionButton.backgroundColor = Color_Clear;
  87. [_actionButton addTarget:self action:@selector(actionButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  88. }
  89. return _actionButton;
  90. }
  91. @end