【问题标题】:How to Adjust the Text Label Color According to the Background Image in Swift如何根据 Swift 中的背景图像调整文本标签颜色
【发布时间】:2020-11-29 11:07:15
【问题描述】:

我在我的项目中使用了很多不同的背景图像,我想知道如何根据视图控制器上的背景图像视图调整 UITextLabel 颜色?即,如果背景较暗,则文本将变为 .white,而如果背景较亮,则文本将变为 .black。任何帮助将不胜感激:)

【问题讨论】:

  • 如果您的背景中有些部分是亮的,而其他部分是暗的,您会怎么做?您可以在某个位置找到像素的颜色。见这里:developer.apple.com/forums/thread/94041。然后你可以平均 textField 框架来决定它是亮的还是暗的。注意:除非你知道背景是什么并且可以定义它是暗还是亮?

标签: ios swift xcode


【解决方案1】:

如果您可以使用第三方库,我建议您使用 https://cocoapods.org/pods/ChameleonFramework 访问它的功能

ContrastColorOf(backgroundColor: UIColor, returnFlat: Bool)

【讨论】:

  • 能否请您告诉我如何使用此框架根据背景为文本字段分配颜色?谢谢!
  • 如果您已经知道背景颜色,可以立即添加到方法中:label.textColor = ContrastColorOf(backgroundColor: YourBackgroundColor, returnFlat: true) 如果您不知道背景的准确颜色,您可以使用Digital Color Meter in您的 Mac 并获取颜色为十六进制值并在方法中使用它:let myColour = UIColor(hexString: "HexValueOfTheBackground") 然后label.textColor = ContrastColorOf(backgroundColor: myColour, returnFlat: true)
猜你喜欢
  • 2020-02-11
  • 2015-12-31
  • 2018-03-18
  • 1970-01-01
  • 1970-01-01
  • 2019-06-23
  • 2013-06-25
  • 2013-06-16
  • 2018-03-14
相关资源
最近更新 更多