简体   繁体   English

如何在UITextField的开头添加空间

[英]How to add space at start of UITextField

I am creating an app that contain multiple UITextField . 我正在创建一个包含多个UITextField的应用程序。 In the textField i have sated border type to none and background image. 在textField中,我将边框类型设置为无和背景图像。 It display fine, But now my text is started from the lest border which does not look good, like this 它显示正常,但现在我的文本是从最小的边框开始,看起来不太好,就像这样

在此输入图像描述

How can i add space at the start of the textfield? 如何在文本字段的开头添加空间?

Try this 试试这个

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

textField.leftView = leftView;

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

Here's @Kalpesh's answer in Swift 3.x : 这是@ 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 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

You should subclass UITextField and override drawText function. 您应该UITextField并覆盖drawText函数。

check this for help OR You can do following: 检查这个以获得帮助或者你可以这样做:

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, height_of_textfiled)];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;

Objective c 目标c

For padding only placeholder this code will work 对于仅填充占位符,此代码将起作用

usernameTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"    Your text"];

For padding both placeholder and text of the UITextField this below code will work 为了填充UITextField的占位符和文本,下面的代码将起作用

usernameTF.layer.sublayerTransform = CATransform3DMakeTranslation(40, 0, 30);

Swift 3.0 Swift 3.0

For padding only placeholder this code will work 对于仅填充占位符,此代码将起作用

yourTextField.attributedPlaceholder = NSAttributedString(string: "    YourText")

For padding both placeholder and text of the UITextField this below code will work 为了填充UITextField的占位符和文本,下面的代码将起作用

usernameTF.layer.sublayerTransform = CATransform3DMakeTranslation(40, 0, 30)

You can subclass UITextField and override textRectForBounds and editingRectForBounds . 您可以textRectForBounds UITextField并覆盖textRectForBoundseditingRectForBounds

For example, 例如,

- (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);
}

斯威夫特4

yourTextField.layer.sublayerTransform = CATransform3DMakeTranslation(10, 0, 10)

您可以将文本域背景图像放在图像视图中,在imageview上方将文本字段留空。

-(void)textField:(UITextField *) textField didBeginEditing {
    if ([textField.text isEqualToString:@""] || textField.text == NULL) {
       textField.text = @" ";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM