对UITextField进行子类化并设置文本字体

时间:2014-09-14 05:41:24

标签: ios uitextfield subclassing

我有一个UITextField的子类,以便我可以将IndexPath项添加到文本字段中,如下所示:

@interface jnTextField : UITextField

@property (nonatomic, retain) NSIndexPath *indexPath;

@end

然后我将textfield添加到InterfaceBuilder中的UITableViewCell。但是现在当我尝试设置文本颜色和字体时,它没有像我使用标准的UITextField那样设置。我需要覆盖什么才能使其正常工作。我无法找到任何解释我所缺少的东西。我用来更改文本字体/颜色/文本的代码是我的cellForRowAtIndexPath方法:

cell.textField.text = @"add url";
cell.textField.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0];
cell.textField.textColor = [UIColor blueColor];

1 个答案:

答案 0 :(得分:1)

问题出在这里......您必须避免使用UIKIT类使用的相同属性名称。

cell.textField.text = @"add url";
cell.textField.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0];
cell.textField.textColor = [UIColor blueColor];

textFieldUITableViewCell类的属性,因此请更改custom UITextField's类中的custom UITabelViewCell(jnTextField)名称。

cell.myCustomTextField.text = @"add url";
cell.myCustomTextField.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0];
cell.myCustomTextField.textColor = [UIColor blueColor];

要实现这一目标,您可能需要像这样使用

MYCustomCell *myCell = (MYCustomCell *)cell

myCell.myCustomTextField.text = @"add url";
myCell.myCustomTextField.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0];
myCell.myCustomTextField.textColor = [UIColor blueColor];

希望它会对你有所帮助。

相关问题