简体   繁体   English

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

[英]iOS: Why does UITextField with setCornerRadius cut off text

I'm trying to load a UITextField with rounded corners into a table view for iOS. 我正在尝试将带圆角的UITextField加载到iOS的表视图中。 For the most part, everything is working fine. 在大多数情况下,一切都很好。 However, the left-most character gets partially cut off due to the corner radius property. 但是,由于角半径属性,最左侧的角色会被部分切断。 Is there a way to set a margin on the text that's inputted into a UITextField, so that it displays properly? 有没有办法在输入到UITextField的文本上设置边距,以便正确显示? Here's my code: 这是我的代码:

        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];

I figured out a solution. 我想出了一个解决方案。 I created a custom textfield, subclassed from UITextField : 我创建了一个自定义文本字段,从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

Use this 用这个

[textInput sizeToFit];

it May Helps you 它可以帮助你

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在iOS中显示为PDF时,具有居中文本的底部表格单元会被切断? - Why does bottom table cell that has centered-text get cut off when displayed as PDF in iOS? 如何阻止 UITextField 的属性占位符文本在 iOS 13 上被截断? - How to stop a UITextField's attributed placeholder text from getting cut off on iOS 13? UITextView 文本被截断 - iOS 10 - UITextView text get cut off - iOS 10 iOS> 7-表格文字被截断 - iOS >7 - Table text is getting cut off 为什么我的 UIwebView 内容在 iOS 中被截断? - Why is my UIwebView content cut off in iOS? 纯文本在 iOS 底部被截断,如何在 xCode 中修复? - Plain text being cut off at bottom in iOS, how to fix in xCode? ios - 列表项目在一定长度后文本被截断 - ios - list items have text cut off after a certain length 如何像移动的横幅一样滚动剪切掉iOS桌面文本 - How to scroll cut off text iOS tableview like a moving banner 为什么UINavigationBar会切断我的部分UIBuilder而不是在测试时将其切断? - Why does the UINavigationBar cut off part of my UIBuilder but not cut it off when tested? 为什么带有图像的按钮使titleLable文本被截断? - Why is a button with an image in it making the titleLable text cut off?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM