2012-05-15 48 views
0

我想要一个具有渐变背景的UITextField,以使我的项目的某个选项更具视觉吸引力。具有渐变背景的iOS UITextField

我想重写UITextField类重写initWithFrame,但它不起作用。我从来没有尝试过继承UIControl,所以我想我错过了一些东西。

有人能帮我一下吗?先谢谢你!

回答

0

你可以尝试这样的回答:iPhone UITextField background color

,并添加一个渐变与CAGradientLayer

+0

它似乎是比我想象的更容易。我所做的一切都很妥当,但是您所附的链接中指出了问题。当继承UITextField时,textField不能是带圆角的。所以最后我分类了UITextField,在initWithFrame上添加了渐变,并将类型设置为无边界。一切工作都很好。还有一点是我正在寻找一个编程式的解决方案,而不必使用图像(JPG,PNG或其他)的梯度。调整大小不是问题。非常感谢你们。 – Marcal

0

UITextField有财产background,这是UIImage类型。因此,您可以在运行时绘制一个UIImage或加载一个准备好的图像并将其设置为您的UITextField的背景。通过这种方式,您可以实现任何您喜欢的文本字段。

1

这应该工作:

textView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"gradient.jpg"]]; 

但你必须改变控制到TextView的。

0

你只是接近该子类是它最好的主意,但并不是覆盖initWithFrame在你的子类,只需使用下面的代码.m文件

- (CGRect)textRectForBounds:(CGRect)bounds 
{ 
    return CGRectInset(bounds, 5, 0); 
} 

- (CGRect)editingRectForBounds:(CGRect)bounds 
{ 
    return CGRectInset(bounds, 5, 0); 
} 

- (void)drawRect:(CGRect)rect 
{ 
    UIImage *textFieldBackground = [[UIImage imageNamed:@"text_field_teal.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(15.0, 5.0, 15.0, 5.0)]; 
    [textFieldBackground drawInRect:[self bounds]]; 
} 

,因为这对我的作品,我认为它会为你的作品 编码快乐:) 享受一天!