UITextField 麻烦居中占位符和更改键盘颜色书写

UITextField troubles centering placeholder and changing the keyboard color writing

我实际上正在为我的 iOs 申请填写注册表。

我对 iOs 还是很陌生,但由于 uiTableViewCell 自定义和 uiTextField,我设法制作了一个漂亮的注册表单。

我现在面临 2 个问题,但我实际上无法解决:

1) 当我将我的 UITextField 居中对齐时,它并没有真正居中或者可能居中但是随后它开始在居中写入我的字符串而不是居中字符串本身,即使我用我的初始化了我的 TextField单元格框架(如果需要,请参见图片)。

2) 我设法更改了 UITextField 文本颜色,但是当键盘显示时,它实际上又开始写黑色而不是白色。我可以更改吗?

抱歉我的英语不好,不是我的母语,但仍在努力,感谢您的帮助

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSString *identifier = @"id";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
   if (cell == nil){
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
   }
   if (indexPath.section == 0){
    self.LastName = [[UITextField alloc] initWithFrame:cell.contentView.bounds];
    self.LastName.placeholder= @"Name";
    self.LastName.autocorrectionType = UITextAutocorrectionTypeNo;
    self.LastName.delegate = self;
    self.LastName.textAlignment = NSTextAlignmentCenter;
    [self.LastName setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];

    [cell.contentView addSubview:self.LastName];
}
else if (indexPath.section == 1)
    cell.textLabel.text = @"Name";

cell.textLabel.textAlignment = NSTextAlignmentCenter;

第一个单元格是我的 UITextField,第二个单元格是我的正常居中单元格(我没有 10 个声誉,所以我只能给你们一个 link 图片,抱歉.. http://s22.postimg.org/sep2kcvrl/not_Working.png

您只是在更改占位符文本的颜色,以更改文本字段中文本的颜色:

 [self.LastName setTextColor:[UIColor whiteColor]];

要将占位符和文本字段的文本居中对齐:

[self.LastName setTextAlignment:NSTextAlignmentCenter];

关于第二个问题:

UITextField 中有 "defaultTextAttributes" 属性 可用于颜色(在 iOS7+ 中可用)。

NSMutableDictionary* textFieldAttrib = [[self.textField defaultTextAttributes] mutableCopy];
textFieldAttrib[@"NSColor"] = [UIColor redColor];
self.textField.defaultTextAttributes = textFieldAttrib;

您也可以通过 "attributedPlaceholder" 属性(在 iOS6+ 中可用)更改占位符颜色。

NSString *placeHolderText = @"some placeholder";
NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString: placeHolderText
                                                                  attributes:@{NSForegroundColorAttributeName : [UIColor greenColor]}];

self.textField.attributedPlaceholder = placeholder;