在UITextField开始加空格

应用中用到多个UITextField,在文本区域的边框类型是没有背景图片的。显示正常,但是文本显示是从最左边开始,看起来很不美观。

CSDN移动问答

想要添加一个起始空格。

UILabel * leftView = [[UILabel alloc] initWithFrame:CGRectMake(10,0,7,26)];
leftView.backgroundColor = [UIColor clearColor];

textField.leftView = leftView;

textField.leftViewMode = UITextFieldViewModeAlways;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

可用图片来替换边框,再添加一个UITextField 放置到图片边框的上层,将UITextfield 设置为无边框类型,这时可任意设置光标显示的位置(实际是在设置UITextfiled的 origin.x)

修改UITextField的.

  • (CGRect)textRectForBounds:(CGRect)bounds{ return CGRectInset(bounds, 5, 0); }
  • (CGRect)editingRectForBounds:(CGRect)bounds{ return CGRectInset(bounds, 5, 0); }