如何简化文本字段的左右填充

时间:2013-09-12 05:22:26

标签: ios objective-c

我已经有了为我的所有文本字段创建填充的解决方案,但我认为我需要简化它,因为我认为这太简单了:

// Padding Left and Right for textfield
UIView *fieldEmail = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldEmail.leftViewMode = UITextFieldViewModeAlways;
_fieldEmail.leftView = fieldEmail;

UIView *fieldPassword = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldPassword.leftViewMode = UITextFieldViewModeAlways;
_fieldPassword.leftView = fieldPassword;

UIView *fieldRegisterName = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldRegisterName.leftViewMode = UITextFieldViewModeAlways;
_fieldRegisterName.leftView = fieldRegisterName;

UIView *fieldRegisterEmail = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldRegisterEmail.leftViewMode = UITextFieldViewModeAlways;
_fieldRegisterEmail.leftView = fieldRegisterEmail;

UIView *fieldRegisterPassword = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldRegisterPassword.leftViewMode = UITextFieldViewModeAlways;
_fieldRegisterPassword.leftView = fieldRegisterPassword;

我们可以创建循环或其他东西,以便这个填充可以影响我的所有文本字段吗?如何简化这个?

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以继承UITextField并覆盖textRectForBoundseditingRectForBoundsdrawPlaceholderInRect。这已在SSToolkit SSTextField中完成。您可以这样使用它:

textField.textEdgeInsets = UIEdgeInsetsMake(0, 0, 10, 10)

如果要对所有文本字段使用相同的插入,则只需对此值进行子类化和编码。