将左边距添加到UITextField [英] Add lefthand margin to UITextField

查看:189
本文介绍了将左边距添加到UITextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 UITextField 的左边距放在10像素。

I want to put the left margin of a UITextField's text at 10 px. What is the best way to do that?

推荐答案

您可以通过扩展 UITextField 类和覆盖两个方法:

You can do it by extending UITextField class and overriding two methods:

- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)editingRectForBounds:(CGRect)bounds;

以下是代码:

接口 MYTextField.h

@interface MYTextField : UITextField

@end



其实现在 MYTextField.m

Its implementation in MYTextField.m

@implementation MYTextField

static CGFloat leftMargin = 28;

 - (CGRect)textRectForBounds:(CGRect)bounds
{
    bounds.origin.x += leftMargin;

    return bounds;
}

 - (CGRect)editingRectForBounds:(CGRect)bounds
{
    bounds.origin.x += leftMargin;

    return bounds;
}
@end

这篇关于将左边距添加到UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆