【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()

分享链接:

工作之余,开了一个淘宝小店,分别销售日常必备生活用品,期待您的光临!点击下图,跳转店铺首页!
原文地址:https://www.cnblogs.com/xjf125/p/14765827.html