[英]iOS: Why does UITextField with setCornerRadius cut off text
I'm trying to load a UITextField with rounded corners into a table view for iOS. 我正在尝试将带圆角的UITextField加载到iOS的表视图中。 For the most part, everything is working fine.
在大多数情况下,一切都很好。 However, the left-most character gets partially cut off due to the corner radius property.
但是,由于角半径属性,最左侧的角色会被部分切断。 Is there a way to set a margin on the text that's inputted into a UITextField, so that it displays properly?
有没有办法在输入到UITextField的文本上设置边距,以便正确显示? Here's my code:
这是我的代码:
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];
I figured out a solution. 我想出了一个解决方案。 I created a custom textfield, subclassed from
UITextField
: 我创建了一个自定义文本字段,从
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
Use this 用这个
[textInput sizeToFit];
it May Helps you 它可以帮助你
textInput.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);
//有助于从给定像素开始编写文本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.