【问题标题】:UITextView to Mimic UITextField [for basic round corner text field]UITextView to Mimic UITextField [用于基本圆角文本字段]
【发布时间】:2013-12-26 20:34:41
【问题描述】:

我想拥有多行的 UITextField,在对这个问题进行快速谷歌搜索后,我发现我应该使用 TextView,所以当我想要多行时,我确实将我的代码切换为使用 UITextView。我的视图还有另一行我想保留的文本字段。

为了让我的 TextView 看起来像 TextField,我必须添加代码来设置边框和半径,但它们在 iOS7 上看起来有点不同。有谁知道:

  • UITextField 边框的颜色是什么?当同时启用和禁用时,我可以发送我的 textview 来匹配它。
  • TextField 角的半径是多少。
  • 禁用时UITextField的背景颜色是什么[附图显示禁用时文本字段有较浅的灰色阴影]?所以当我禁用用户交互时,我可以将我的文本视图设置为相同的颜色。

如果无法继续使用文本字段来处理多行文本,我会全力以赴并转而使用它。

最好的问候,

【问题讨论】:

  • 有人知道这个问题吗?
  • 为什么不使用单行 UITextView 而不是 UITextField。这样您就可以自定义它们以使其看起来相同。

标签: colors ios7 uitextfield uitextview


【解决方案1】:

我用这个:

textView.layer.borderColor = [[UIColor colorWithRed:215.0 / 255.0 green:215.0 / 255.0 blue:215.0 / 255.0 alpha:1] CGColor];
textView.layer.borderWidth = 0.6f;
textView.layer.cornerRadius = 6.0f;

参数的细微差别使它看起来更像 UITextField(我希望)。

【讨论】:

  • 这在视网膜显示器上看起来要准确得多。
【解决方案2】:

我用这个:

#import <QuartzCore/QuartzCore.h>

-(void) textViewLikeTextField:(UITextView*)textView
{
    [textView.layer setBorderColor:[[UIColor colorWithRed:212.0/255.0
                                                                       green:212.0/255.0
                                                                        blue:212.0/255.0
                                                                       alpha:1] CGColor]];
    [textView.layer setBorderWidth:1.0f];
    [textView.layer setCornerRadius:7.0f];
    [textView.layer setMasksToBounds:YES];
}

并取得好成绩。

【讨论】:

  • 我用过这个,但下面@Hamhog 的修改更准确一些。
【解决方案3】:

我有一个 UITextView 的小子类,它在 iOS 7 中的外观与 UITextField 相同

界面为空:

@interface MyTextView : UITextView
@end

实现覆盖了“可编辑”属性的初始化和设置器:

@implementation MyTextView

//================================================================================

- (id) initWithFrame: (CGRect) frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self commonInit];
    }
    return self;
}

//================================================================================

- (id) initWithCoder: (NSCoder *) aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self commonInit];
    }
    return self;
}

//================================================================================

- (void) commonInit
{
    self.layer.borderWidth  = 1.0f;
    self.layer.borderColor  = [[UIColor colorWithRed:232.0/255.0
                       green:232.0/255.0 blue:232.0/255.0 alpha:1] CGColor];
    self.layer.cornerRadius = 6;
}

//================================================================================

- (void) setEditable: (BOOL) editable
{
    [super setEditable:editable];

    if (editable)
    {
        self.backgroundColor = [UIColor whiteColor];
    }
    else
    {
        self.backgroundColor = [UIColor colorWithRed:250.0/255.0
                                green:250.0/255.0 blue:250.0/255.0 alpha:1];
    }
}

//================================================================================

@end

【讨论】:

    【解决方案4】:

    这是我启用 UITextView 后得到的最接近的结果

    [yourTextView.layer setBorderColor:[[[UIColor lightGrayColor] colorWithAlphaComponent:0.2] CGColor]];
    [yourTextView.layer setBorderWidth:2.0];
    yourTextView.layer.cornerRadius = 5;
    yourTextView.clipsToBounds = YES;
    yourTextView.textColor = [UIColor lightGrayColor];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多