向 UITextField 添加左边距 [英] Add lefthand margin to UITextField
本文介绍了向 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
类而不是使用类别,因此您可以在所需的文本字段上明确使用它.
As I have explained in a previous comment, the best solution in this case is to extend the UITextField
class instead of using a category, so you can use it explicitly on the desired text fields.
#import <UIKit/UIKit.h>
@interface MYTextField : UITextField
@end
@implementation MYTextField
- (CGRect)textRectForBounds:(CGRect)bounds {
int margin = 10;
CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
return inset;
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
int margin = 10;
CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
return inset;
}
@end
类别旨在向现有类添加新函数,而不是覆盖现有方法.
A category is intended to add new functions to an existing class, not to override an existing method.
这篇关于向 UITextField 添加左边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文