Tour4x Tour4x - 8 months ago 30
iOS Question

UITextfield don't show the content when editing

enter image description here

Hi,I have get a problem.
I custom a textfield like the second textfield in the pic,override the function

-(void)drawRect:(CGRect)rect {

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(self.h / 2, self.h / 2)];
path.lineWidth = 0.5;
[[UIColor lightGrayColor] setStroke];
[path stroke];


-(CGRect)textRectForBounds:(CGRect)bounds {

return CGRectMake(self.h / 2, self.h, self.w - self.h, self.h);

When I editing in the custom textfield,the content is not show in it,and i end edit it,the content show. The first textfield is the system style that works fine.
Hope someone could help me solve the problem, tks very much.

- (CGRect)textRectForBounds:(CGRect)bounds {
  return CGRectOffset(bounds, 0, 0);

- (CGRect)placeholderRectForBounds:(CGRect)bounds {
  return [self textRectForBounds:bounds];

- (CGRect)editingRectForBounds:(CGRect)bounds {
  return [self textRectForBounds:bounds];

Use constants instead of zeros to set custom offset in your code