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

标签 ios uitextfield rounded-corners

我正在尝试将带有圆角的 UITextField 加载到 iOS 的表格 View 中。在大多数情况下,一切都运行良好。但是,由于角半径属性,最左边的字符会被部分截断。有没有办法在输入到 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];

最佳答案

我想出了一个解决办法。我创建了一个自定义文本字段,它是 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

关于iOS:为什么带有 setCornerRadius 的 UITextField 会截断文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14079266/

相关文章:

ios - Watchkit 扩展的嵌套框架问题

ios - 大小与字体 : in iOS 7

ios - timescale 和 fps 有什么区别?

iphone - 删除最后一个字符 UITextField

ios - SKView 集成到 UIViewController 中,UITextFields 滞后

css - Internet Explorer 使用图像的圆 Angular 颜色配置文件问题

macos - NSButton 圆角半径和 NSPopover

ios - 如何在没有全屏模式的情况下播放视频?

ios - 如何从 iOS 键盘内部检测 UITextInput 设置

ios - 如何在 UIView swift 3 的两侧添加圆角