SearchCommonBarView.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // CommonBarView.m
  3. // SeeTheDay
  4. //
  5. // Created by lww on 2022/6/25.
  6. // Rectangle 4
  7. #import "SearchCommonBarView.h"
  8. @interface SearchCommonBarView()<UITextFieldDelegate>
  9. @end
  10. @implementation SearchCommonBarView
  11. - (instancetype)initWithFrame:(CGRect)frame
  12. {
  13. self = [super initWithFrame:frame];
  14. if (self) {
  15. [self configUI];
  16. }
  17. return self;
  18. }
  19. -(void)configUI{
  20. self.backgroundColor = rgba(18, 23, 41, 1);
  21. self.bgView = [UIView new];
  22. [self addSubview:self.bgView];
  23. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  24. make.bottom.equalTo(self).offset(-6);
  25. make.left.right.equalTo(self);
  26. make.height.mas_equalTo(@36);
  27. }];
  28. self.searchBgView =[UIView new];
  29. self.searchBgView.backgroundColor = rgba(52, 56, 68, 1);
  30. self.searchBgView.layer.cornerRadius = 18;
  31. self.searchBgView.layer.masksToBounds = true;
  32. [self.bgView addSubview:self.searchBgView];
  33. [self.searchBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.top.bottom.equalTo(self.bgView);
  35. make.left.equalTo(self.bgView).offset(20);
  36. make.right.equalTo(self.bgView).offset(-64);
  37. }];
  38. self.tipimageView = [UIImageView new];
  39. self.tipimageView.image = [UIImage imageNamed:@"Frame 9345"];
  40. [self.searchBgView addSubview:self.tipimageView];
  41. [self.tipimageView mas_makeConstraints:^(MASConstraintMaker *make) {
  42. make.centerY.equalTo(self.searchBgView);
  43. make.left.equalTo(self.searchBgView).offset(12);
  44. make.size.mas_equalTo(CGSizeMake(20, 20));
  45. }];
  46. self.textField = [UITextField new];
  47. NSMutableAttributedString *attributedPlaceholder = [[NSMutableAttributedString alloc] initWithString:@"搜索"];
  48. [attributedPlaceholder addAttribute:NSForegroundColorAttributeName value:rgba(255, 255, 255, 0.4) range:NSMakeRange(0, attributedPlaceholder.length)];
  49. [attributedPlaceholder addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:14.0] range:NSMakeRange(0, attributedPlaceholder.length)];
  50. self.textField.attributedPlaceholder = attributedPlaceholder;
  51. // self.textField.text = @"扫黑行动";
  52. self.textField.textColor = [UIColor whiteColor];
  53. self.textField.font = [UIFont systemFontOfSize:14];
  54. self.textField.delegate = self;
  55. [self.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  56. self.textField.returnKeyType = UIReturnKeySearch;
  57. [self.searchBgView addSubview:self.textField];
  58. [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.centerY.equalTo(self.searchBgView);
  60. make.left.equalTo(self.tipimageView.mas_right).offset(8);
  61. make.height.right.equalTo(self.searchBgView);
  62. }];
  63. self.searchButton = [UIButton new];
  64. [self.searchButton setTitle:@"搜索" forState:UIControlStateNormal];
  65. self.searchButton.titleLabel.font = [UIFont systemFontOfSize:14];
  66. [self.searchButton setTitleColor:rgba(255, 212, 0, 1) forState:UIControlStateNormal];
  67. [self.searchButton addTarget:self action:@selector(searchBtnAction) forControlEvents:UIControlEventTouchUpInside];
  68. [self.bgView addSubview:self.searchButton];
  69. [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
  70. make.centerY.equalTo(self.searchBgView);
  71. make.left.equalTo(self.searchBgView.mas_right).offset(16);
  72. }];
  73. }
  74. - (void)textFieldDidChange:(UITextField *)textField
  75. {
  76. if(textField.text.length>10){
  77. textField.text = [textField.text substringToIndex:10];
  78. }
  79. }
  80. -(void)searchBtnAction{
  81. if(self.searchAction && self.textField.text.length>0){
  82. self.searchAction(self.textField.text);
  83. }
  84. }
  85. - (BOOL)textFieldShouldReturn:(UITextField *)textField{
  86. if (textField.text.length != 0) {
  87. self.searchAction(self.textField.text);
  88. }
  89. [textField resignFirstResponder];
  90. return YES;
  91. }
  92. @end