我正在创建一个包含多个UITextField
的应用。在textField中,我将边框类型设置为无和背景图像。它显示正常,但现在我的文本是从最小的边框开始,看起来不太好,就像这个
如何在文本字段的开头添加空格?
答案 0 :(得分:19)
试试这个
UILabel * leftView = [[UILabel alloc] initWithFrame:CGRectMake(10,0,7,26)];
leftView.backgroundColor = [UIColor clearColor];
textField.leftView = leftView;
textField.leftViewMode = UITextFieldViewModeAlways;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
答案 1 :(得分:8)
这是@ Kalpesh在 Swift 3.x 中的回答:
let leftView = UILabel(frame: CGRectMake(10, 0.0, 7, 26))
leftView.backgroundColor = .clearColor()
customField.leftView = leftView
customField.leftViewMode = .Always
customField.contentVerticalAlignment = .Center
Swift 4.x
let leftView = UILabel(frame: CGRect(x: 10, y: 0, width: 7, height: 26))
leftView.backgroundColor =.clear
customField.leftView = leftView
customField.leftViewMode = .always
customField.contentVerticalAlignment = .center
答案 2 :(得分:7)
你应该继承UITextField
并覆盖drawText函数。
检查this以获取帮助 要么 你可以这样做:
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, height_of_textfiled)];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
答案 3 :(得分:6)
对于仅填充占位符,此代码可以使用
usernameTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" Your text"];
对于填充UITextField的占位符和文本,以下代码将起作用
usernameTF.layer.sublayerTransform = CATransform3DMakeTranslation(40, 0, 30);
对于仅填充占位符,此代码可以使用
yourTextField.attributedPlaceholder = NSAttributedString(string: " YourText")
对于填充UITextField的占位符和文本,以下代码将起作用
usernameTF.layer.sublayerTransform = CATransform3DMakeTranslation(40, 0, 30)
答案 4 :(得分:4)
您可以继承UITextField
并覆盖textRectForBounds
和editingRectForBounds
。
例如,
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 5, bounds.size.width - 20, bounds.size.height - 10);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 5, bounds.size.width - 20, bounds.size.height - 10);
}
答案 5 :(得分:4)
雨燕4
yourTextField.layer.sublayerTransform = CATransform3DMakeTranslation(10, 0, 10)
答案 6 :(得分:0)
您可以将文本域背景图像放在图像视图中,在图像视图上方,将文本字段留下一个空格。
答案 7 :(得分:-1)
-(void)textField:(UITextField *) textField didBeginEditing {
if ([textField.text isEqualToString:@""] || textField.text == NULL) {
textField.text = @" ";
}
}