ios - HUD 中的文本颜色
问题描述
这是我的代码:
override func draw(_ rect: CGRect) {
let boxWidth: CGFloat = 96
let boxHeight: CGFloat = 96
let boxRect = CGRect(x: round((bounds.size.width - boxWidth) / 2),
y: round((bounds.size.height - boxHeight) / 2),
width: boxWidth, height: boxHeight)
let roundedRect = UIBezierPath(roundedRect: boxRect, cornerRadius: 10)
UIColor(white: 0.3, alpha: 0.8).setFill()
roundedRect.fill()
if let image = UIImage(named: "Checkmark") {
let imagePoint = CGPoint(
x: center.x - round(image.size.width / 2),
y: center.y - round(image.size.height / 2) - boxHeight / 8)
image.draw(at: imagePoint)
}
let attribs = [ kCTFontAttributeName: UIFont.systemFont(ofSize: 66.0), kCTForegroundColorAttributeName: UIColor.white]
let textSize = text.size(withAttributes: attribs as [NSAttributedString.Key : Any])
let textPoint = CGPoint(
x: center.x - round(textSize.width / 2),
y: center.y - round(textSize.height / 2) + boxHeight / 4)
text.draw(at: textPoint, withAttributes: attribs as [NSAttributedString.Key : Any])
}
HUD 中的文本颜色为黑色。为什么以及如何将其更改为白色?
解决方案
您应该使用NSAttributedString.Key
而不是CoreText
键尝试替换它:
let attribs = [ kCTFontAttributeName: UIFont.systemFont(ofSize: 66.0), kCTForegroundColorAttributeName: UIColor.white]
有了这个:
let attribs: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 66.0),
.foregroundColor: UIColor.green]
推荐阅读
- mysql - MySQL 在运行时更新表的空值
- jestjs - 带有 jest.config.js 的 Jest cli testPathIgnorePatterns 导致未找到测试
- c# - 在一个线程上串行运行任务
- sqlite - 检索 sqlite 表中的出现次数
- python - 是否可以修改 Apache Spark JDBC 连接器的查询?
- python - 在任意范围内切片 n 维 numpy 数组
- python - 无法在 Mac 上安装 XGBoost-1.0.0
- python - 熊猫:所有行的循环
- node.js - 即使我在 url 中输入值,也表达参数未定义
- python - 熊猫计算日期之间的值的累积和(来自另一个df)