我正在尝试将带有圆角的 UITextField 加载到 iOS 的表格 View 中。在大多数情况下,一切都运行良好。但是,由于角半径属性,最左边的字符会被部分截断。有没有办法在输入到 UITextField 的文本上设置边距,以便它正确显示?这是我的代码:
textInput = [[UITextField alloc] init];
textInput.backgroundColor = [UIColor whiteColor];
textInput.placeholder = @"example@gmail.com";
textInput.textColor = [UIColor blackColor];
textInput.keyboardType = UIKeyboardTypeEmailAddress;
textInput.returnKeyType = UIReturnKeyDone;
[[textInput layer] setBorderColor:[[UIColor whiteColor] CGColor]];
[[textInput layer] setBorderWidth:2.3];
[[textInput layer] setCornerRadius:15];
[textInput setClipsToBounds: YES];
[textInput setDelegate:self];
[self.contentView addSubview:textInput];
[textInput release];
最佳答案
我想出了一个解决办法。我创建了一个自定义文本字段,它是 UITextField
的子类:
#import "CR_customTextField.h"
@implementation CR_customTextField
- (CGRect)textRectForBounds:(CGRect)bounds {
int margin = 10;
CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
return inset;
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
int margin = 10;
CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
return inset;
}
@end
关于iOS:为什么带有 setCornerRadius 的 UITextField 会截断文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14079266/