【问题标题】:Changing textField border color iOS [duplicate]更改文本字段边框颜色iOS [重复]
【发布时间】:2013-02-15 15:34:44
【问题描述】:

我想更改 UITextField 边框 颜色。是否可以自定义边框颜色?我搜索了xib 中的所有选项,但没有找到任何更改border color 的选项。

【问题讨论】:

  • 抱歉,当我搜索文本字段边框颜色更改时,我发现了那个帖子,所以我发布了,否则我不会发布。

标签: ios objective-c uitextfield xib


【解决方案1】:

你可以用这个:

yourTextField.layer.borderColor=[[UIColor blackColor]CGColor];

yourTextField.layer.borderWidth=1.0;

记得在你的 .h 文件中导入#import <QuartzCore/QuartzCore.h>

您还可以指定 RGB 值。

yourTextField.layer.borderColor=[[UIColor colorWithRed:178.0f/255.0f green:178.0f/255.0f blue:178.0f/255.0f alpha:1.0] CGColor];

注意:您需要从 iOS 7 开始设置这两个值

swift 2.3 更新

yourTextField.layer.borderColor = UIColor.blackColor().CGColor
yourTextField.layer.borderWidth = 1.0

yourTextField.layer.borderColor = UIColor(red: 178.0 / 255.0, green: 178.0 / 255.0, blue: 178.0 / 255.0, alpha: 1.0).CGColor

swift 3.1.1 更新

yourTextField.layer.borderColor = UIColor.black.cgColor
yourTextField.layer.borderWidth = 1.0

yourTextField.layer.borderColor = UIColor(red: 178.0 / 255.0, green: 178.0 / 255.0, blue: 178.0 / 255.0, alpha: 1.0).cgColor

【讨论】:

  • 在IOS7上好像不行
  • 它只适用于borderWidth。即使在 iOS6 中,您也必须提供borderWidth。它也适用于 iOS7。
  • 为此苦苦挣扎了大约 10 分钟!最后,我似乎没有将 textField 连接到插座(
  • 发生@trickster77777 :)
  • 在答案中可能值得注意的是,您需要在 iOS 7 和 iOS 8 中同时设置 borderColorborderWith 才能使颜色真正改变。
【解决方案2】:
#import <QuartzCore/QuartzCore.h>

使用下面的代码来改变文本框的边框颜色

textField.layer.borderWidth = 2.0f;
textField.layer.borderColor = [[UIColor redColor] CGColor];
textField.layer.cornerRadius = 5;
textField.clipsToBounds      = YES;

对于 SWIFT:

textField.layer.borderColor = UIColor.redColor().CGColor;

【讨论】:

  • txt_field.layer.borderColor = UIColor.redColor().CGColor // 对于 Swift
【解决方案3】:

使用 Quartzcore 框架。 你可以设置textField.layer.borderWidth,以及borderColor:

tField.layer.borderColor = [UIColor redColor].CGColor;

更多参考: https://stackoverflow.com/a/5749376/1554632

【讨论】:

    猜你喜欢
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 2013-02-19
    • 2017-12-29
    • 1970-01-01
    • 2023-01-31
    相关资源
    最近更新 更多