TDSearchBar.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // TDSearchBar.m
  3. // TheoryNetwork
  4. //
  5. // Created by tederen on 2019/9/23.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "TDSearchBar.h"
  9. @implementation TDSearchBar
  10. - (instancetype)initWithFrame:(CGRect)frame
  11. {
  12. self = [super initWithFrame:frame];
  13. if (self) {
  14. self.backgroundImage = [UIImage new];
  15. self.placeholder = @" 搜索";
  16. self.layer.cornerRadius = 18.f;
  17. self.layer.borderWidth = 0.5;
  18. self.layer.borderColor = [UIColor hexStringToColor:@"d8d8d8"].CGColor;
  19. UIImage *searchIcon = [UIImage imageNamed:@"sousuo"];
  20. [self setImage:searchIcon forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
  21. for (UIView *view in self.subviews.lastObject.subviews.lastObject.subviews) {
  22. if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
  23. UITextField *textField = (UITextField *)view;
  24. //设置输入框的背景颜色
  25. textField.clipsToBounds = YES;
  26. //设置输入字体颜色
  27. textField.textColor = kColorFromRGB(0x0a0a0a);
  28. //设置默认文字颜色
  29. textField.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
  30. NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:self.placeholder];
  31. [placeholder addAttribute:NSForegroundColorAttributeName value:UIColorHex(0xA9A9A9) range:NSMakeRange(0, self.placeholder.length)];
  32. [placeholder addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, self.placeholder.length)];
  33. textField.attributedPlaceholder = placeholder;
  34. }
  35. }
  36. UIImage* clearImg = [UtilsTools imageWithColor:[UIColor clearColor] andHeight:20.f];
  37. [self setSearchFieldBackgroundImage:clearImg forState:UIControlStateNormal];
  38. }
  39. return self;
  40. }
  41. - (instancetype)init
  42. {
  43. self = [super init];
  44. // if (self) {
  45. // self.backgroundImage = [UIImage new];
  46. // self.placeholder = @" 搜索";
  47. // self.layer.cornerRadius = 5.f;
  48. // self.layer.borderWidth = 0.5;
  49. // self.layer.borderColor = [UIColor hexStringToColor:@"d8d8d8"].CGColor;
  50. // UIImage *searchIcon = [UIImage imageNamed:@"sousuo"];
  51. // [self setImage:searchIcon forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
  52. // for (UIView *view in self.subviews.lastObject.subviews.lastObject.subviews) {
  53. // if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
  54. // UITextField *textField = (UITextField *)view;
  55. // //设置输入框的背景颜色
  56. // textField.clipsToBounds = YES;
  57. // //设置输入字体颜色
  58. // textField.textColor = kColorFromRGB(0x333333);
  59. // //设置默认文字颜色
  60. //
  61. // textField.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
  62. // }
  63. // }
  64. // UIImage* clearImg = [UtilsTools imageWithColor:[UIColor clearColor] andHeight:20.f];
  65. // [self setSearchFieldBackgroundImage:clearImg forState:UIControlStateNormal];
  66. // }
  67. if (self) {
  68. self.backgroundImage = [UIImage new];
  69. self.placeholder = @" 搜索";
  70. self.layer.cornerRadius = 18.f;
  71. self.layer.borderWidth = 0.5;
  72. self.layer.borderColor = [UIColor hexStringToColor:@"d8d8d8"].CGColor;
  73. UIImage *searchIcon = [UIImage imageNamed:@"sousuo"];
  74. [self setImage:searchIcon forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
  75. for (UIView *view in self.subviews.lastObject.subviews.lastObject.subviews) {
  76. if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
  77. UITextField *textField = (UITextField *)view;
  78. //设置输入框的背景颜色
  79. textField.clipsToBounds = YES;
  80. //设置输入字体颜色
  81. textField.textColor = kColorFromRGB(0x0a0a0a);
  82. //设置默认文字颜色
  83. textField.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
  84. NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:self.placeholder];
  85. [placeholder addAttribute:NSForegroundColorAttributeName value:UIColorHex(0xA9A9A9) range:NSMakeRange(0, self.placeholder.length)];
  86. [placeholder addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, self.placeholder.length)];
  87. textField.attributedPlaceholder = placeholder;
  88. }
  89. }
  90. UIImage* clearImg = [UtilsTools imageWithColor:[UIColor clearColor] andHeight:20.f];
  91. [self setSearchFieldBackgroundImage:clearImg forState:UIControlStateNormal];
  92. }
  93. return self;
  94. }
  95. - (void)setCusPlaceholder:(NSString *)text
  96. {
  97. for (UIView *view in self.subviews.lastObject.subviews.lastObject.subviews) {
  98. if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
  99. UITextField *textField = (UITextField *)view;
  100. //设置输入框的背景颜色
  101. textField.clipsToBounds = YES;
  102. //设置输入字体颜色
  103. textField.textColor = kColorFromRGB(0x0a0a0a);
  104. //设置默认文字颜色
  105. textField.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
  106. NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:text];
  107. [placeholder addAttribute:NSForegroundColorAttributeName value:UIColorHex(0xA9A9A9) range:NSMakeRange(0, text.length)];
  108. [placeholder addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, text.length)];
  109. textField.attributedPlaceholder = placeholder;
  110. }
  111. }
  112. }
  113. @end