【Swift Mac开发】开发日常小记录Tips
1、禁止NSViewController更改大小
override func viewDidAppear() { super.viewDidAppear() view.window!.styleMask.remove(.resizable) }
2、更改NSTextField的背景颜色
更改NSTextField的背景颜色与NSView的不同,不需要Wantslayer
textField.backgroundColor = NSColor.lightGray
3、设置NSImageView的填充样式
import Foundation import Cocoa class BMBaseImageView: NSImageView { override var image: NSImage? { set { self.layer = CALayer() //设置图片的填充样式 self.layer?.contentsGravity = CALayerContentsGravity.resizeAspectFill self.layer?.contents = newValue self.wantsLayer = true super.image = newValue } get { return super.image } } }
4、NSTextField不允许输入中文
let cell : NSTextFieldCell = pwdTextField.cell! as! NSTextFieldCell cell.allowedInputSourceLocales = [NSAllRomanInputSourcesLocaleIdentifier]
5、NSBezierPath绘制线条
let myPath = NSBezierPath() myPath.move(to: CGPoint(x: drawDatePoint.x + dateAttrString.size().width / 2, y: 0)) myPath.line(to: CGPoint(x: drawDatePoint.x + dateAttrString.size().width / 2, y: drawDatePoint.y)) NSColor.init(hexRGB: 0xf5f5f5).set() myPath.stroke()