![](/img/trans.png)
[英]How to add UILongPressGestureRecognizer to a UITextField?
[英]How to add space at start of UITextField
我正在創建一個包含多個UITextField
的應用程序。 在textField中,我將邊框類型設置為無和背景圖像。 它顯示正常,但現在我的文本是從最小的邊框開始,看起來不太好,就像這樣
如何在文本字段的開頭添加空間?
試試這個
UILabel * leftView = [[UILabel alloc] initWithFrame:CGRectMake(10,0,7,26)];
leftView.backgroundColor = [UIColor clearColor];
textField.leftView = leftView;
textField.leftViewMode = UITextFieldViewModeAlways;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
這是@ 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
您應該UITextField
並覆蓋drawText函數。
檢查這個以獲得幫助或者你可以這樣做:
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, height_of_textfiled)];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
對於僅填充占位符,此代碼將起作用
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)
您可以textRectForBounds
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);
}
斯威夫特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.