0

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

我正在尝试继承 UITextField 类来覆盖 initWithFrame,但它不起作用。我以前从未尝试过对 UIControl 进行子类化,所以我想我错过了一些东西。

有人可以帮帮我吗?先感谢您!

4

4 回答 4

1

这应该有效:

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

但是您必须将控件更改为 TextView。

于 2012-05-15T10:47:44.400 回答
0

UITextFieldhas 属性,background它是 type UIImage。因此,您可以UIImage在运行时绘制或加载准备好的图像并将其设置为 UITextField 的背景。通过这种方式,您可以为文本字段实现任何您喜欢的外观。

于 2012-05-15T10:46:18.760 回答
0

你可以试试这个答案:iPhone UITextField 背景颜色

并使用 CAGradientLayer 添加渐变

于 2012-05-15T10:57:28.353 回答
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]];
}

因为这对我有用,我认为它对你有用 快乐编码:) 享受这一天!

于 2012-05-15T11:47:36.413 回答