Ios UITextField与leftview对齐
我有一个UItextField,在左视图上有一个图像,但我需要UItextField与中心对齐,在左视图中,它向右移动。 代码如下:Ios UITextField与leftview对齐,ios,objective-c,uitextfield,alignment,Ios,Objective C,Uitextfield,Alignment,我有一个UItextField,在左视图上有一个图像,但我需要UItextField与中心对齐,在左视图中,它向右移动。 代码如下: self.usernameField = [[UITextField alloc]initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, 50)]; self.usernameField.placeholder = @"E-mail"; [self.usernameField setFont:[UI
self.usernameField = [[UITextField alloc]initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, 50)];
self.usernameField.placeholder = @"E-mail";
[self.usernameField setFont:[UIFont ralewayRegularFontWithSize:20]];
[self.usernameField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.usernameField setKeyboardType:UIKeyboardTypeEmailAddress];
[self.usernameField setBackground:[UIImage imageNamed:@"background_placeholder"]];
[self.usernameField setTextColor:[UIColor whiteColor]];
[self.usernameField setTextAlignment:NSTextAlignmentCenter];
[self.usernameField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[self.usernameField setBorderStyle:UITextBorderStyleNone];
[self.usernameField setBackgroundColor:[UIColor clearColor]];
[self.usernameField setLeftViewMode:UITextFieldViewModeAlways];
self.usernameField.leftView = [[UIView alloc] init];
[self.usernameField.leftView setFrame:CGRectMake(0, 0, 50, 30)];
[self.usernameField.leftView setBackgroundColor:[UIColor clearColor]];
UIImageView *imgViewMail = [[UIImageView alloc] initWithFrame:CGRectMake(20, 5, 25, 25)];
[imgViewMail setImage:[UIImage imageNamed:@"mail_icon"]];
[self.usernameField.leftView addSubview:imgViewMail];
[self.view addSubview:self.usernameField];
编辑-说明问题的图像。-前两个文本字段
您可以创建自定义UITextField,并覆盖以下方法:
#pragma mark - Override UITextField
- (CGRect) textRectForBounds: (CGRect) bounds
{
float x = bounds.origin.x + 50.f;
float y = bounds.origin.y;
float width = bounds.size.width - (100.f);
float height = bounds.size.height;
return CGRectMake(x, y, width, height);
}
- (CGRect) editingRectForBounds: (CGRect) bounds
{
return [self textRectForBounds: bounds];
}
您可以发布任何图像来确定确切的问题吗?k,因为您说电子邮件和senha应该居中右侧?它应该居中,向左多个像素。但是,它应该是
self.usernameField=[[UITextField alloc]…
而不是self.usernameField=[[textfield自定义alloc]…
(自定义UITextField的名称)