【问题标题】:Swift - Drawing text with drawInRect:withAttributes:Swift - 使用 drawInRect:withAttributes 绘制文本:
【发布时间】:2014-10-05 10:44:25
【问题描述】:

我对 Xcode 6.1 GM 有一个奇怪的问题。

let text: NSString = "A"
let font = NSFont(name: "Helvetica Bold", size: 14.0)

let textRect: NSRect = NSMakeRect(5, 3, 125, 18)
let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle
textStyle.alignment = NSTextAlignment.LeftTextAlignment
let textColor = NSColor(calibratedRed: 0.147, green: 0.222, blue: 0.162, alpha: 1.0)

let textFontAttributes = [
    NSFontAttributeName: font,
    NSForegroundColorAttributeName: textColor,
    NSParagraphStyleAttributeName: textStyle
]

text.drawInRect(NSOffsetRect(textRect, 0, 1), withAttributes: textFontAttributes)

Error is in line let texFontAttributes...

Cannot convert the expression's type 'Dictionary' to type 'DictionaryLiteralConvertible'

此代码在 Xcode 6.1 GM 之前可以完美运行。

当我尝试将 textFontAttributes 声明为 NSDictionary 时,错误消息更改为:

Cannot convert the expression's type 'NSDictionary' to type 'NSString!'

我不知道如何解决这个问题:(

【问题讨论】:

  • 我不知道为什么,但 drawAtPoint:withAttributes:drawInRect:withAttributes:drawWithRect:options:attributes:sizeWithAttributes:boundingRectWithSize:options:attributes: 是“在 Swift 中不可用”
  • @JDS 它在 Swift 中不可用,但在 Swift 的 String 类型中不可用。您可以像 OP 一样在 NSString 上调用这些方法。

标签: swift


【解决方案1】:

问题在于font 是可选的,因为便利构造函数现在返回可选值,因此需要将font 解包为字典中的值:

if let actualFont = font {
    let textFontAttributes = [
        NSFontAttributeName: actualFont,
        NSForegroundColorAttributeName: textColor,
        NSParagraphStyleAttributeName: textStyle
    ]

    text.drawInRect(NSOffsetRect(textRect, 0, 1), withAttributes: textFontAttributes)
}

【讨论】:

  • 是否可以在 swift 中使用 draw in rect 将渐变颜色应用于具有属性的文本?你也可以查看question
【解决方案2】:

Swift 4

    let attributeDict: [NSAttributedString.Key : Any] = [
        .font: font!,
        .foregroundColor: textColor,
        .paragraphStyle: textStyle,
    ]

    text.draw(in: rect, withAttributes: attributeDict)

【讨论】:

  • 现在NSAttributedString,Key
  • 是否可以在 swift 中使用 draw in rect 将渐变颜色应用于具有属性的文本?你也可以查看question
【解决方案3】:

这也是另一种选择。

let textFontAttributes = [
    NSFontAttributeName : font!,
    NSForegroundColorAttributeName: textColor,
    NSParagraphStyleAttributeName: textStyle
]

【讨论】:

    【解决方案4】:

    我的应用中有这段代码可以正常运行:

        var textAttributes: [String: AnyObject] = [
            NSForegroundColorAttributeName : UIColor(white: 1.0, alpha: 1.0).CGColor,
            NSFontAttributeName : UIFont.systemFontOfSize(17)
        ]
    

    【讨论】:

    • 在 iOS 8 中,使用 .drawAtPoint:withAttributes 时,.CGColor 会导致崩溃。它需要一个 UIColor。
    • @bitmusher ty,我的应用程序因神秘原因而崩溃,您的评论让我找到了正确的原因。 UIColor,而不是 CGColor。你会认为编译器可以捕捉到这一点,但是那些打包到字符串数组中的属性来自 Swift 有点危险,否则它非常挑剔和明确。
    【解决方案5】:

    在 XCode 12、Swift 5.3 中。我找不到 String 对象的 draw 方法。 在我手动转换为 NSString 后,它对我有用。

    let text: String = "XXX"
    (text as NSString).draw(in: rect withAttributes: attributes)
    

    【讨论】:

      猜你喜欢
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多