Xcode Swift - 属性文本不适用于字体属性

时间:2017-07-30 18:22:15

标签: swift nsattributedstring

编译器不断抛出此错误:“没有更多上下文,表达式类型不明确”。它将它扔在:let textFontAttributes = [NSAttributedString。有人能指出我正确的方向。谢谢。

    let textRect = CGRect(x: 50, y: 60, width: 56, height: 35)
    let textTextContent = String(globalWorkoutCount) + " / 7"
    let textStyle = NSMutableParagraphStyle()
    textStyle.alignment = .center
    let textFontAttributes = [NSAttributedString: UIFont(name: "Avenir-Medium", size: 24)!, NSForegroundColorAttributeName: UIColor.white, NSMutableParagraphStyle: textStyle]

    let textTextHeight: CGFloat = textTextContent.boundingRect(with: CGSize(width: textRect.width, height: CGFloat.infinity), options: .usesLineFragmentOrigin, attributes: textFontAttributes, context: nil).height
    context.saveGState()
    context.clip(to: textRect)
    textTextContent.draw(in: CGRect(x: textRect.minX, y: textRect.minY + (textRect.height - textTextHeight) / 2, width: textRect.width, height: textTextHeight), withAttributes: textFontAttributes)
    context.restoreGState()

1 个答案:

答案 0 :(得分:1)

NSAttributedString更改为NSFontAttributeName,将NSMutableParagraphStyle更改为NSParagraphStyleAttributeName,它应该有效。

这应该是最终的结果:

let textFontAttributes = [NSFontAttributeName: UIFont(name: "Avenir-Medium", size: 24)!, NSForegroundColorAttributeName: UIColor.white, NSParagraphStyleAttributeName: textStyle]