UITextField调整内容位置及左右视图LeftView、RightView

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 清除按钮左移一个单元
- (CGRect)clearButtonRectForBounds:(CGRect)bounds
{
    CGRect rect = [super clearButtonRectForBounds:bounds];
    if (_isReservedRightView){
        rect = CGRectOffset(rect, -rect.size.width, 0);
    }
    return rect;
}
 
// 控制 placeHolder 的位置,左右缩 8
- (CGRect)textRectForBounds:(CGRect)bounds
{
    CGRect rect = [super textRectForBounds:bounds];
    return CGRectInset(rect, 8, 0);
}
 
// 控制文本的位置,左右缩 8
- (CGRect)editingRectForBounds:(CGRect)bounds
{
    CGRect rect = [super textRectForBounds:bounds];
    return CGRectInset(rect, 8, 0);
}
 
// 重写方法,固定viewMode为Always
- (void)setLeftView:(UIView *)leftView
{
    [super setLeftView:leftView];
    self.leftViewMode = UITextFieldViewModeAlways;
}

注意:clearButton即rightView,如果设置了rightView,clearButton就不会再出现!

Leave a Reply