iOS---searchBar 搜索框 光标初始位置后移

Posted 张文权的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS---searchBar 搜索框 光标初始位置后移相关的知识,希望对你有一定的参考价值。

  1. #import <UIKit/UIKit.h>
  2. @interface SearchBar : UITextField
  3. @property (nonatomic,strong) UIButton *button;
  4. + (instancetype)searchBar;
  5. @end
  6. #import "SearchBar.h"
  7. @implementation SearchBar
  8. - (id)initWithFrame:(CGRect)frame
  9. {
  10. self = [super initWithFrame:frame];
  11. if (self) {
  12. self.font = [UIFont systemFontOfSize:15];
  13. self.placeholder = @" 输入品种关键字";
  14. //设置边框和边框颜色
  15. self.borderStyle=UITextBorderStyleNone;
  16. self.layer.cornerRadius=15.0f;
  17. self.layer.masksToBounds=YES;
  18. self.layer.borderColor=[[UIColor colorWithRed:224/255.0 green:243/255.0 blue:223/255.0 alpha:1.0]CGColor];
  19. self.layer.borderWidth= 2.0f;
  20. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
  21. [button setBackgroundImage:[UIImage imageNamed:@"seachBar_rightView"] forState:UIControlStateNormal];
  22. [button setTitle:@"搜索" forState:UIControlStateNormal];
  23. [button setTitleColor:[UIColor colorWithRed:119/255.0 green:166/255.0 blue:16/255.0 alpha:1.0] forState:UIControlStateNormal];
  24. button.titleLabel.font = [UIFont systemFontOfSize:18.0];
  25. button.titleLabel.textColor = [UIColor colorWithRed:119/255.0 green:166/255.0 blue:16/255.0 alpha:1.0];
  26. button.titleLabel.textColor = [UIColor redColor];
  27. self.rightView = button;
  28. self.rightViewMode = UITextFieldViewModeAlways;
  29. //这里设置光标位置,让光标位置后移10
  30. self.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
  31. self.leftViewMode = UITextFieldViewModeAlways;
  32. }
  33. return self;
  34. }
  35. + (instancetype)searchBar
  36. {
  37. return [[self alloc] init];
  38. }
  39. @end
+
 

以上是关于iOS---searchBar 搜索框 光标初始位置后移的主要内容,如果未能解决你的问题,请参考以下文章

如何用js实现输入框上下光标选择相应项目

自定义searchview的编辑框,搜索按钮,删除按钮,光标等

微信小程序6位支付密码输入框

增删改查测试点

SDUT 3571 Password 暴力搜索

Android 自定义的验证码输入框(无光标),android版本10暂时不支持自定义粘贴