ios - HUD 中的文本颜色

标签 ios swift

这是我的代码:

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 keys 尝试替换这个:

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]

关于ios - HUD 中的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56734550/

相关文章:

swift - CoreMIDI 目标的困惑

Xcode 快速链接到 Facebook 页面

iOS - 将输入流的委托(delegate)设置为另一个类

ios - 当我使用数组时 Xcode 8 永远索引

ios - AVAudioPlayer 不播放音频

ios - 当我移动 View 的框架时它不会移动 : why?

iOS 可移动覆盖屏幕

ios - 即使找到字符串后也要遍历整个数组

ios - Onfido 沙箱错误 422 无法处理的实体验证

ios - 面向协议(protocol)的编程不会带来继承主要解决的代码重复问题吗?