【问题标题】:custom UITextField with image as a background blinking cursor issue以图像为背景的自定义 UITextField 闪烁光标问题
【发布时间】:2011-09-08 02:43:41
【问题描述】:

我有一个以 UIImage 作为背景的自定义 UITextField。现在,当我在这个 UITextField 中输入文本并且光标闪烁时,我得到了这个:

我不想让光标上的白色图像闪烁,因为它破坏了美感。 我尝试使用它并尝试将背景视图设置为与图像具有相同的颜色,但问题是 UITextField 形状始终是矩形。我该如何解决这个问题?

【问题讨论】:

    标签: iphone objective-c ipad uitextfield


    【解决方案1】:

    你试过设置textField.backgroundColor = [UIColor clearColor]吗?

    您可以为图像使用 UIImageView,然后添加 UITextField 作为具有清晰背景的子视图。只需以编程方式或在 IB 中创建 UIImageView *imageViewUITextField *textField。如果您以编程方式进行,请使用[imageView addSubview:textField]; 并根据需要设置框架。在 IB 中,只需将 textField 放到 imageView 上,然后随意对齐即可。

    【讨论】:

    • 是的,我确实尝试过,但我仍然得到白色背景。我该如何选择第二种选择?介意显示一些代码吗?
    • 我似乎无法在 IB 的 imageView 上删除 textField
    • 我也尝试以编程方式添加子视图,但没有成功,因为我在 UIImage 中没有看到任何 UITextField,我尝试了bringSubviewIntoFront,但效果不佳.. 请建议
    • @adit 这个答案是否解决了问题?你已经接受了,但是从你的 cmets 看来问题没有解决。我有一个非常相似的问题,希望有一个解决方案。
    【解决方案2】:

    我刚试过。它没有像那样突出显示光标。

    设置

    textField.backgroundColor = [UIColor brownColor] // or your color
    

    textField.textColor = [UIColor whiteColor];
    

    用于将 textField 添加到 UIView ;

    //Do this where you create the UIView* view
    
    
        UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)];
        textField.clearsOnBeginEditing = NO;
        textField.textAlignment = UITextAlignmentRight;
        textField.returnKeyType = UIReturnKeyDone;
        textField.textColor = [UIColor blackColor]; // or any other color
    
        textField.font = [UIFont systemFontOfSize:15];
        textField.delegate = self;
    
    
        [view addSubview:textField];
    

    【讨论】:

    • 但是现在 UITextField 的形状是一个矩形,它没有我的 UIImage 的形状
    • 我已经添加了在 UIview 中包含文本字段的代码
    【解决方案3】:

    如何创建一个包含您的图像的UIImageView,然后在您的图像视图中嵌入一个无边界的UITextField?文本字段仍可编辑,但不应干扰其后面的图像视图。

    【讨论】:

      猜你喜欢
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 2012-12-11
      相关资源
      最近更新 更多