Swift - Drawing text with drawInRect:withAttributes:

前端 未结 5 623
自闭症患者
自闭症患者 2020-12-14 08:15

I have strange problem with Xcode 6.1 GM.

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

let textRect: NSRect = NSMakeRe         


        
5条回答
  •  长情又很酷
    2020-12-14 09:11

    The problem is that font is optional because the convenience contructors now return optional values, so font needs to be unwrapped to be a value in your dictionary:

    if let actualFont = font {
        let textFontAttributes = [
            NSFontAttributeName: actualFont,
            NSForegroundColorAttributeName: textColor,
            NSParagraphStyleAttributeName: textStyle
        ]
    
        text.drawInRect(NSOffsetRect(textRect, 0, 1), withAttributes: textFontAttributes)
    }
    

提交回复
热议问题