小编iPe*_*era的帖子

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

我对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)
Run Code Online (Sandbox Code Playgroud)

错误是在行中让texFontAttributes ...

Cannot convert the expression's type 'Dictionary' to type 'DictionaryLiteralConvertible'
Run Code Online (Sandbox Code Playgroud)

这段代码完美地运行到Xcode 6.1 GM.

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

Cannot convert the expression's type …
Run Code Online (Sandbox Code Playgroud)

swift

20
推荐指数
2
解决办法
3万
查看次数

Swift 2.0 constraintsWithVisualFormat

我在Swift 2.0中遇到了AutoLayout的奇怪问题

self.webView = WKWebView()
self.view.addSubview(self.webView!)

var viewBindingsDict: NSMutableDictionary = NSMutableDictionary()
viewBindingsDict.setValue(self.webView, forKey: "webView")

self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: 0, metrics: nil, views: viewBindingsDict))

self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[webView]|", options: 0, metrics: nil, views: viewBindingsDict))
Run Code Online (Sandbox Code Playgroud)

我收到错误:

'String' is not convertible to 'StringLiteralConvertible'
Run Code Online (Sandbox Code Playgroud)

"H:| [webView] |" "V:| [webView] |"

我不知道我是否遗漏了什么

更新: 操场截图

autolayout swift swift2

12
推荐指数
1
解决办法
7242
查看次数

Swift中的子类NSApplication

我创建了NSApplication子类:

class MyApplication: NSApplication {
    override func sendEvent(theEvent: NSEvent) {
        if theEvent.type == NSEventType.KeyUp && (theEvent.modifierFlags & .CommandKeyMask).rawValue != 0 {
            self.keyWindow?.sendEvent(theEvent)
        } else {
            super.sendEvent(theEvent)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

之后,我将Info.plist中的"Principal class"更改为MyApplication,最后在Main.storyboard的Application Scene中,我也将Application更改为MyApplication.

当我运行应用程序时,我得到以下消息:

class MyApplication: NSApplication {
    override func sendEvent(theEvent: NSEvent) {
        if theEvent.type == NSEventType.KeyUp && (theEvent.modifierFlags & .CommandKeyMask).rawValue != 0 {
            self.keyWindow?.sendEvent(theEvent)
        } else {
            super.sendEvent(theEvent)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮我弄这个吗?

nsapplication swift

11
推荐指数
1
解决办法
2351
查看次数

标签 统计

swift ×3

autolayout ×1

nsapplication ×1

swift2 ×1