UITextField 搜索框

Textfield.png

UITextField 搜索框滿足你的需求,使用如下

參考源碼

CoustomTextField \*textfieid = [[CoustomTextField alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 28) leftViewMargin:10 textColor:[UIColor redColor] bgColor:[UIColor yellowColor] pholderText:@"請輸入您要搜索的內(nèi)容" leftViewImage:@"" rightViewImage:@""];
改變TextField LeftView,RightView間距

  • (CGRect)borderRectForBounds:(CGRect)bounds;
  • (CGRect)textRectForBounds:(CGRect)bounds;
  • (CGRect)placeholderRectForBounds:(CGRect)bounds;
  • (CGRect)editingRectForBounds:(CGRect)bounds;
  • (CGRect)clearButtonRectForBounds:(CGRect)bounds;
  • (CGRect)leftViewRectForBounds:(CGRect)bounds;
  • (CGRect)rightViewRectForBounds:(CGRect)bounds;
實現(xiàn)方式

// 設置光標顏色

define TINTCOLOR [UIColor redColor]

@interface CoustomTextField ()
/** 左邊間距 */
@property (nonatomic,assign) CGFloat leftMargin;
@end
@implementation CoustomTextField

// 左邊view的 frame

  • (CGRect)leftViewRectForBounds:(CGRect)bounds
    {
    CGRect iconRect = [super leftViewRectForBounds:bounds];
    iconRect.origin.x += self.leftMargin;
    return iconRect;
    }

  • (CGRect)editingRectForBounds:(CGRect)bounds{
    CGRect editingRect = [super editingRectForBounds:bounds];
    editingRect.origin.x += 10;
    editingRect.size.width -= 2 * self.leftMargin;
    return editingRect;
    }

  • (CGRect)textRectForBounds:(CGRect)bounds{
    CGRect textRect = [super textRectForBounds:bounds];
    textRect.origin.x += 10;
    return textRect;
    }

  • (CGRect)rightViewRectForBounds:(CGRect)bounds{
    CGRect iconRect = [super rightViewRectForBounds:bounds];
    iconRect.origin.x -= self.leftMargin;
    return iconRect;
    }

  • (CGRect)placeholderRectForBounds:(CGRect)bounds{
    CGRect placeholdRect = [super placeholderRectForBounds:bounds];
    return placeholdRect;
    }
    -(instancetype)initWithFrame:(CGRect)frame leftViewMargin:(CGFloat)leftViewMargin textColor:(UIColor *)textColor bgColor:(UIColor *)bgColor pholderText:(NSString *)pholderText leftViewImage:(NSString *)imageName rightViewImage:(NSString *)rimageName{

    self = [super initWithFrame:frame];
    if (self) {

      _leftMargin = leftViewMargin;
      self.borderStyle = UITextBorderStyleNone;
      self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
      self.backgroundColor = bgColor;
      self.layer.cornerRadius = 7.f;
      self.layer.borderColor = self.backgroundColor.CGColor;
      self.textColor = textColor;
      self.font = [UIFont systemFontOfSize:14];
      self.placeholder = pholderText;
      self.tintColor = TINTCOLOR;
      UIImage *leftImage = [UIImage imageNamed:imageName.length?imageName :@"searchicon"];
      UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, leftImage.size.width, leftImage.size.height)];
      leftImageView.tintColor = [UIColor whiteColor];
      leftImageView.contentMode = UIViewContentModeCenter;
      leftImageView.image = leftImage;
     
      UIImage \*rightImage = [UIImage imageNamed:rimageName.length?rimageName:@"deleteinput"];
      
      UIButton \*rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
      [rightButton setImage:rightImage forState:UIControlStateNormal];
      rightButton.frame = CGRectMake(frame.size.width - 20, 0,leftImage.size.width, leftImage.size.height);
      [rightButton addTarget:self action:@selector(clearTextFiled) forControlEvents:UIControlEventTouchUpInside];
      [rightButton setImage:rightImage forState:UIControlStateHighlighted];
      rightButton.tintColor = [UIColor whiteColor];
      rightButton.contentMode = UIViewContentModeCenter;
      self.leftView = leftImageView;
      self.leftViewMode = UITextFieldViewModeAlways;
      self.returnKeyType = UIReturnKeySearch;
    
      self.rightView = rightButton;
      self.rightViewMode = UITextFieldViewModeAlways;      
    

    }
    return self;
    }

  • (void)clearTextFiled{
    self.text = nil;
    }

  • (void)setPholderColor:(UIColor *)pholderColor{
    _pholderColor = pholderColor;
    [self setValue:pholderColor forKeyPath:@"_placeholderLabel.textColor"];
    }

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容