Jom*_*t17 5 iphone uitextfield xcode4 ios5
首先,我需要设置占位符文本颜色,因此我将UITextfield子类化为几个堆栈溢出帖子中显示的内容.这很好用.下面是一个示例文章:如何子类化UITextField并覆盖drawPlaceholderInRect以更改占位符颜色.
但是,当我尝试使用时searchNameField.textAlignment = UITextAlignmentCenter;它不再是占位符的中心.输入文本仍然很好.
那么,假设由于子类化而使居中不起作用,我必须添加到我的UITextField子类以使占位符文本居中?
谢谢
编辑
这是我用来解决这个问题的代码.这是放在我的UITextField的子类中.
- (CGRect)placeholderRectForBounds:(CGRect)bounds
{
CGSize size = [[self placeholder] sizeWithFont:[UIFont fontWithName:@"Arial" size:placeholdTextSize]];
return CGRectMake( (bounds.size.width - size.width)/2 , bounds.origin.y , bounds.size.width , bounds.size.height);
}
Run Code Online (Sandbox Code Playgroud)
请注意,placeholdTextSize是我在初始化期间设置的属性.
伙伴们,伙计
子类化UITextField将完成工作:
// CustomTextField.h
@interface CustomTextField : UITextField {
}
@end
Run Code Online (Sandbox Code Playgroud)
覆盖方法:
@implementation
- (CGRect)placeholderRectForBounds:(CGRect)bounds {
return CGRectMake(x,y,width,height);//Return your desired x,y position and width,height
}
- (void)drawPlaceholderInRect:(CGRect)rect {
//draw place holder.
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:12]];
}
@end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5005 次 |
| 最近记录: |