(IOS)UITextField文字周围增加边距

// 子类化UITextField,增加insert属性

@interfaceWZBTextField :UITextField

@property(nonatomic, assign)UIEdgeInsetsinsets;

@end

// 在.m文件重写下列方法

-(CGRect)textRectForBounds:(CGRect)bounds {

    CGRect paddedRect = UIEdgeInsetsInsetRect(bounds,self.insets);

    if(self.rightViewMode == UITextFieldViewModeAlways ||self.rightViewMode == UITextFieldViewModeUnlessEditing) {

        return[selfadjustRectWithWidthRightView:paddedRect];

    }

    returnpaddedRect;

}

-(CGRect)placeholderRectForBounds:(CGRect)bounds {

    CGRect paddedRect = UIEdgeInsetsInsetRect(bounds,self.insets);


    if(self.rightViewMode == UITextFieldViewModeAlways ||self.rightViewMode == UITextFieldViewModeUnlessEditing) {

        return[selfadjustRectWithWidthRightView:paddedRect];

    }

    returnpaddedRect;

}

-(CGRect)editingRectForBounds:(CGRect)bounds {

    CGRect paddedRect = UIEdgeInsetsInsetRect(bounds,self.insets);

    if(self.rightViewMode == UITextFieldViewModeAlways ||self.rightViewMode == UITextFieldViewModeWhileEditing) {

        return[selfadjustRectWithWidthRightView:paddedRect];

    }

    returnpaddedRect;

}

-(CGRect)adjustRectWithWidthRightView:(CGRect)bounds {

    CGRect paddedRect = bounds;

    paddedRect.size.width -= CGRectGetWidth(self.rightView.frame);


    returnpaddedRect;

}

你可能感兴趣的:((IOS)UITextField文字周围增加边距)