Ios 3X4带圆形按钮的键盘,如iPhone手机应用程序中所示,适用于所有屏幕尺寸

Ios 3X4带圆形按钮的键盘,如iPhone手机应用程序中所示,适用于所有屏幕尺寸,ios,swift,iphone,autolayout,uikit,Ios,Swift,Iphone,Autolayout,Uikit,有谁能帮我打破苹果公司创造世界的做法吗?我曾经尝试过用1:1的按钮和许多其他方法创建它,但都是徒劳的。我想在我自己的项目中使用这样的屏幕进行学习 这是我想要的,也是我目前拥有的,解决方案的一部分可能涉及自定义(圆形、绿色)按钮。要创建红色背景的按钮(作为对关键操作的警告),我将NSButton子类化为: class ColorButton: NSButton 以及它的绘制(…)方法 这让我可以控制自己的外表。从NSButton继承的所有其他行为 此外,@IBDesignable和@IBInsp

有谁能帮我打破苹果公司创造世界的做法吗?我曾经尝试过用1:1的按钮和许多其他方法创建它,但都是徒劳的。我想在我自己的项目中使用这样的屏幕进行学习


这是我想要的,也是我目前拥有的,

解决方案的一部分可能涉及自定义(圆形、绿色)按钮。要创建红色背景的按钮(作为对关键操作的警告),我将NSButton子类化为:

class ColorButton: NSButton
以及它的绘制(…)方法

这让我可以控制自己的外表。从NSButton继承的所有其他行为

此外,@IBDesignable和@IBInspectable标记公开了InterfaceBuilder中的自定义控件

这是最简单的部分。获得恰到好处的外观(对我来说)是一项不平凡的工作,因为可可按钮有一些微妙的行为(例如禁用、突出显示)。响应桌面颜色选项(如暗模式)也是一个考虑因素

下面是我的ColorButton的代码,它再次改变了背景颜色而不是形状

@IBDesignable
class ColorButton: NSButton {

    @IBInspectable var backgroundColor: NSColor = NSColor.controlBackgroundColor
    { didSet { needsDisplay = true } }

    @IBInspectable var textColor: NSColor = NSColor.controlTextColor

    override func draw(_ dirtyRect: NSRect)
    {   
        //  fill in background with regular or highlight color
        var backgroundColor = (isHighlighted ? self.backgroundColor.highlight(withLevel: 0.75) : isEnabled ? self.backgroundColor : NSColor.gray)!
        if  isEnabled == false      { backgroundColor = NSColor.lightGray }
        let textColor = (isEnabled ? self.textColor : NSColor.disabledControlTextColor )
        var rect: NSRect = NSRect(x: 7.0, y: 5.0, width: self.frame.width - 14.0, height: self.frame.height - 13.0) // self.frame//   dirtyRect
        var path = NSBezierPath(roundedRect: rect, xRadius: 4.0, yRadius: 4.0)
        backgroundColor.setFill()
        NSColor.darkGray.setStroke()
        path.lineWidth = 0
        path.fill()
        path.stroke()

        let font = NSFont(name: "Helvetica", size: (rect.height) * 0.66)
        let text = title as NSString
        let textFontAttributes = [convertFromNSAttributedStringKey(NSAttributedString.Key.font): font!, convertFromNSAttributedStringKey(NSAttributedString.Key.foregroundColor): textColor] as [String : Any]
        let size = text.size(withAttributes: convertToOptionalNSAttributedStringKeyDictionary(textFontAttributes))
        let location = NSPoint(x: ((self.frame.width - size.width) / 2.0), y: ((self.frame.height - size.height) / 2.0) - 3.0)
        text.draw(at: location, withAttributes: convertToOptionalNSAttributedStringKeyDictionary(textFontAttributes))
    }// draw
} 
以上是在Swift 3下开发的。增加了Swift 4迁移工具:

// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertFromNSAttributedStringKey(_ input: NSAttributedString.Key) -> String {
    return input.rawValue
}

// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertToOptionalNSAttributedStringKeyDictionary(_ input: [String: Any]?) -> [NSAttributedString.Key: Any]? {
    guard let input = input else { return nil }
    return Dictionary(uniqueKeysWithValues: input.map { key, value in (NSAttributedString.Key(rawValue: key), value)})
}


解释得很好,也有代码。

您需要显示您尝试过的内容,并显示结果如何与您期望的不符。我将尝试使用UIKit框架中的颜色、按钮、字体、边框路径和点?这是前所未闻的。