加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift – 使用drawInRect绘制文本:withAttributes:

发布时间:2020-12-14 05:37:54 所属栏目:百科 来源:网络整理
导读:我有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 NSMutab
我有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,1),withAttributes: textFontAttributes)

错误在行中让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!'

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

问题是字体是可选的,因为方便的引用者现在返回可选值,所以字体需要被解开为字典中的值:
if let actualFont = font {
    let textFontAttributes = [
        NSFontAttributeName: actualFont,NSParagraphStyleAttributeName: textStyle
    ]

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读