iOS:为什么带有setCornerRadius的UITextField会切断文本

时间:2012-12-29 06:24:13

标签: ios uitextfield rounded-corners

我正在尝试将带圆角的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];

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

使用此

[textInput sizeToFit];

它可以帮助你

答案 2 :(得分:0)

   textInput.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);

//有助于开始从给定像素中编写文本