【问题标题】:iOS: Why does UITextField with setCornerRadius cut off textiOS:为什么带有 setCornerRadius 的 UITextField 会截断文本
【发布时间】:2012-12-29 06:24:13
【问题描述】:

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

【问题讨论】:

    标签: ios uitextfield rounded-corners


    【解决方案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
    

    【讨论】:

    【解决方案2】:

    使用这个

    [textInput sizeToFit];
    

    它可以帮助你

    【讨论】:

    • 大大缩小了 UITextField 的大小,并且没有做任何事情来使输入的文本更加可见。
    【解决方案3】:
       textInput.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-22
      • 2015-02-08
      • 1970-01-01
      • 2016-05-10
      • 2013-08-29
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多