ios - 如何通过界面生成器在属性字符串中使用动态颜色?
问题描述
根据在 iOS 上实现暗模式,我们需要将 foregroundColor 属性设置为新的标签颜色。这是如何使用界面生成器完成的?
我尝试使用“文本颜色”选项并将颜色设置为 Developer->labelColor,但这不起作用。
编辑:由于目前这是不可能的,我使用了这个解决方法:
override func viewDidLoad() {
// Support Dark Mode
if #available(iOS 13.0, *) {
let attributes = NSMutableAttributedString(attributedString: textView.attributedText!)
attributes.addAttribute(.foregroundColor, value: UIColor.label, range: NSRange(location: 0, length: attributes.mutableString.length))
textView.attributedText = attributes
}
}
解决方案
您不能在 Interface Builder 中执行此操作。您必须.foregroundColor
在代码中设置属性。
let mas = NSMutableAttributedString(string:s, attributes:[
.font:UIFont(name:"GillSans", size:15)!,
.foregroundColor:UIColor.label,
]
推荐阅读
- ios - firebase 是否可以根据电话数据或其他表单创建用户,避免在应用程序上注册以进行身份验证?
- c++-cli - 如何在 C++/CLI 中包装 C++ boost::variant 以授予来自 .NET 的访问权限
- java - wait() 不会保存应用程序的进度
- java - 如何在函数内创建一个变量,以保存其值从调用到调用
- php - Doctrine 中的自定义多对多关系
- django - 一种通过 Flower(或其他类似界面)查看特定任务日志的方法
- uri - 使用“?”更好吗?或“;” 在网址中?
- bazel - 如何让我的自定义标头模板规则传递它的输出下游 cc_binary/cc_library 依赖项?
- postgis - postgis - ST_Intersects 不工作,不同的测量系统
- android - 如何使禁用 EditText 和 Button 的布局可点击