Quang Phạm Công
Quang Phạm Công

Reputation: 253

Custom UITextField like this

enter image description here

I must custom UITextField like the image above, so i try to write my custom class extend UITextField class. But my problem is: stroke shadow outline of rect, and set white background for textfield inside my custom code is:

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

So my question is: what code i'm missing(here is set white background inside, set outline shadow for border) thanks for your helps!

Upvotes: 0

Views: 5240

Answers (3)

Quang Phạm Công
Quang Phạm Công

Reputation: 253

Finally, i solved my problems after reading some tutorials, here is my code i've written

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

Upvotes: 0

Vincent Bernier
Vincent Bernier

Reputation: 8664

Have you try this first?
self.textField.background = myUIImage;
self.textField.borderStyle = UITextBorderStyleNone;

Upvotes: 2

Bergasms
Bergasms

Reputation: 2203

Use the image of the text field box, and put it behind a normal UITextField that is set to custom and is therefore transparent. That way you get the text field functionality and can put any sort of graphic behind it.

Upvotes: 0

Related Questions