3

我正在尝试将带有圆角的 UITextField 加载到 iOS 的表格视图中。在大多数情况下,一切正常。但是,由于角半径属性,最左边的字符会被部分截断。有没有办法在输入到 UITextField 的文本上设置边距,以便正确显示?这是我的代码:

        textInput = [[UITextField alloc] init];
        textInput.backgroundColor = [UIColor whiteColor];
        textInput.placeholder = @"example@gmail.com";
        textInput.textColor = [UIColor blackColor];
        textInput.keyboardType = UIKeyboardTypeEmailAddress;
        textInput.returnKeyType = UIReturnKeyDone;

        [[textInput layer] setBorderColor:[[UIColor whiteColor] CGColor]];
        [[textInput layer] setBorderWidth:2.3];
        [[textInput layer] setCornerRadius:15];
        [textInput setClipsToBounds: YES];
        [textInput setDelegate:self];

    [self.contentView addSubview:textInput];
        [textInput release];
4

3 回答 3

1

我想出了一个解决办法。我创建了一个自定义文本字段,子类来自UITextField

#import "CR_customTextField.h"

@implementation CR_customTextField

- (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
于 2013-01-09T19:14:10.727 回答
0

用这个

[textInput sizeToFit];

它可以帮助你

于 2012-12-29T06:28:14.623 回答
0
   textInput.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);

//帮助从给定像素开始写入文本

于 2014-11-28T07:01:33.303 回答