ios - 像这样自定义 UITextField

标签 ios uitextfield

enter image description here

我必须像上图一样自定义 UITextField,所以我尝试编写我的自定义类扩展 UITextField 类。 但我的问题是:矩形的描边阴影轮廓,并为里面的文本字段设置白色背景 我的自定义代码是:

- (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];
 }

所以我的问题是:我缺少什么代码(这里是在里面设置白色背景,为边框设置轮廓阴影)感谢您的帮助!

最佳答案

你先试试这个吗?
self.textField.background = myUIImage;<br/> self.textField.borderStyle = UITextBorderStyleNone;

关于ios - 像这样自定义 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13756664/

相关文章:

ios - KVO keyPathsForValuesAffecting 不起作用

ios - CLGeocoder 错误。 GEOErrorDomain 代码=-3

ios - 使用 UITextViews 作为 UITextFields

ios - 将第一响应者从 uitextfield 更改为 uitextview [iOS 7] 时出现死锁

ios - 可以在 iOS 14 上使用 keyboardType emailAddress 在 UITextField 上隐藏预测文本栏吗?

ios - 将文本字段值存储在字典中

ios - 向下或向上滑动 MKMapView 指定数量的像素

ios - 位于顶部的 UITableView header

ios - UIView动画完成回调?

ios - 使用动画在 UITextField 中移动文本