UITextField自定义背景视图和转换文本 [英] UITextField custom background view and shifting text
问题描述
我正在尝试使用自定义文本字段背景.问题在于该文本在左侧似乎太靠近了.
I'm trying to use a custom textfield background. The problem is that the text then appears to be too close on the left.
在不继承UITextField的子类的情况下,我看不到任何移动文本的方法.所以我正在尝试扩展和覆盖
I don't see any way to shift the text without subclassing UITextField. So I'm attempting to extend and overwrite
- (void)drawTextInRect:(CGRect)rect{
NSLog(@"draw rect");
CGRect newRect = CGRectMake(rect.origin.x+20,rect.origin.y,rect.size.width-20,rect.size.height);
[super drawTextInRect:newRect];
}
但是由于某些原因,日志从不打印.我知道正在使用子类,因为我在init中也有一个日志,并且可以正常打印.
But for some reason the log never prints. I know the subclass is being used since I also have a log in the init, and that prints fine.
打开地面
编辑.
我也尝试
- (CGRect)textRectForBounds:(CGRect)bounds{
NSLog(@"bounds");
CGRect b = [super textRectForBounds:bounds];
b.origin.x += 20;
return b;
}
这实际上可以追溯,但似乎并没有改变
That actually traces, but it doesn't seem to be shifting
推荐答案
我认为您可以为此使用 leftView
属性.
I think you could use the leftView
property for this.
您可以将 leftView
和 rightView
添加到UITextfield.这些视图可用于显示图标,但是如果为空视图,则仅会占用空间,这就是您想要的.
You can add a leftView
and rightView
to a UITextfield. These views can be used to display an icon, but if it's an empty view it'll just take up space, which is what you want.
CGFloat leftInset = 5.0f;
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, leftInset, self.bounds.size.height)];
self.leftView = leftView;
self.leftViewMode = UITextFieldViewModeAlways;
[leftView release];
这篇关于UITextField自定义背景视图和转换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!