【问题标题】:UITextField Aligment with leftviewUITextField 与左视图对齐
【发布时间】:2014-03-31 03:30:06
【问题描述】:

我有一个 UItextField,在 leftView 上有一个图像,但我需要 UITextField 与中心对齐,并且在左视图中,它向右移动。 代码如下:

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];

编辑 - 图片说明问题。 - 两个 First textFields

【问题讨论】:

  • 您可以发布任何图片来确定确切的问题吗?
  • k 你说 E-mail 和 senha 应该居中对吗?
  • 它应该居中,向左更多像素。

标签: ios objective-c uitextfield alignment


【解决方案1】:

您可以创建自定义 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];
}

【讨论】:

  • 但是,应该是 self.usernameField = [[TextFieldCustom alloc]...(您的自定义 UITextField 的名称)而不是 self.usernameField = [[UITextField alloc]...
猜你喜欢
  • 2018-12-07
  • 1970-01-01
  • 2013-04-17
  • 2016-09-24
  • 2012-02-24
  • 1970-01-01
  • 2012-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多