【问题标题】:Set the placeholder text colour of a UISearchField in iOS 13在 iOS 13 中设置 UISearchField 的占位符文本颜色
【发布时间】:2020-01-06 04:47:18
【问题描述】:

我正在尝试在 iOS 13(测试版)中设置 UISearchField 的占位符文本颜色。

我的代码在 iOS 12 及之前版本中运行。我仍然可以更改 UISearchField 背景颜色、搜索图标的颜色等等...

这是我正在尝试的:

searchTextField.attributedPlaceholder = NSAttributedString(
  string: "My placeholder text",
  attributes: [
    NSAttributedString.Key.foregroundColor: UIColor.red
  ]
);

我希望占位符文本现在是红色的,但它仍然是灰色的。

【问题讨论】:

  • 顺便说一句,您的代码工作正常。看来你的问题出在另一点。可能你不调用这个代码
  • @dmitry:该线程中的所有建议(几乎所有建议都与上述相同)在 iOS 13 中均无效。正在调用代码,我正在使用一个最小的示例。如果它对您有用,那么您可能没有使用 iOS 13。
  • 可能你是对的。我使用 iOS 12.2 进行测试
  • 好吧,正如标题和帖子本身所说,此问题仅影响 iOS 13。
  • 为 Xcode11 和 iOS 13 检查这个: stackoverflow.com/a/58375484/7851805

标签: ios swift cocoa-touch ios13


【解决方案1】:

你可以如下使用 NSMutableAttributeString,

  let attrString = NSMutableAttributedString(string: "your placeHolder")

    attrString.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.red, range: NSRange(location: 0, length: attrString.length))

searchTextField.attributedPlaceholder = attrString

【讨论】:

  • 这对我没有任何影响。我们应该期待它吗?- 属性字符串的可变性在这里似乎并不重要。
猜你喜欢
  • 2020-01-09
  • 2020-02-22
  • 2011-08-15
  • 2016-12-19
  • 1970-01-01
  • 2014-06-03
  • 1970-01-01
  • 1970-01-01
  • 2014-11-02
相关资源
最近更新 更多