我必须像上面的图片一样自定义UITextField,所以我尝试编写我的自定义类扩展UITextField类。 但我的问题是:笔划阴影轮廓的rect,并为textfield里面设置白色背景 我的自定义代码是:
- (void)drawRect:(CGRect)rect
{
// Drawing code
self.autocorrectionType = UITextAutocorrectionTypeNo;
self.autocapitalizationType = UITextAutocapitalizationTypeNone;
CALayer *layer = self.layer;
layer.cornerRadius = 15.0;
layer.masksToBounds = YES;
layer.borderWidth = 1.0;
layer.borderColor = [[UIColor colorWithRed:0 green:0 blue:0 alpha:1] CGColor];
[layer setShadowColor: [[UIColor blackColor] CGColor]];
[layer setShadowOpacity:1];
[layer setShadowOffset: CGSizeMake(2.0, 2.0)];
[self setClipsToBounds:NO];
[self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
}
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x + 20, bounds.origin.y + 8,
bounds.size.width - 40, bounds.size.height - 16);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
所以我的问题是:我缺少什么代码(这里设置了白色背景,为边框设置轮廓阴影)感谢您的帮助!
答案 0 :(得分:2)
你先试试这个吗?
self.textField.background = myUIImage;
self.textField.borderStyle = UITextBorderStyleNone;
答案 1 :(得分:0)
使用文本字段框的图像,并将其放在设置为自定义的普通UITextField后面,因此是透明的。这样您就可以获得文本字段功能,并可以在其后面放置任何类型的图形。
答案 2 :(得分:0)
最后,我在阅读了一些教程后解决了我的问题,这是我编写的代码
- (void)drawRect:(CGRect)rect
{
self.autocorrectionType = UITextAutocorrectionTypeNo;
self.autocapitalizationType = UITextAutocapitalizationTypeNone;
CALayer *layer = self.layer;
layer.backgroundColor = [[UIColor whiteColor] CGColor];
layer.cornerRadius = 15.0;
layer.masksToBounds = YES;
layer.borderWidth = 1.0;
layer.borderColor = [[UIColor colorWithRed:0 green:0 blue:0 alpha:1] CGColor];
[layer setShadowColor: [[UIColor blackColor] CGColor]];
[layer setShadowOpacity:1];
[layer setShadowOffset: CGSizeMake(0, 2.0)];
[layer setShadowRadius:5];
[self setClipsToBounds:NO];
[self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
}
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x + 20, bounds.origin.y + 8,
bounds.size.width - 40, bounds.size.height - 16);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}