这是实现Font枚举的好方法吗?

时间:2016-09-07 07:49:29

标签: ios swift enums

我有两种实现方式,我无法弄清楚哪一种更好,或者可能存在其他更好的方式。请给我建议。

如果有人有兴趣:

let date1 = NSDate()
Fonts1.Avenir.AvenirNext.Heavy(15)
print(-date1.timeIntervalSinceNow * 1000)
let date2 = NSDate()
Fonts.AvenirNext(.Heavy).getFont(15)
print(-date2.timeIntervalSinceNow * 1000)

我得到了结果:

7.06201791763306
0.348985195159912

Fonts.HelveticaNeue.Bold(15)

enum Fonts {
    enum Default: HelveticaNeueFontProtocol {
        static var familyName: String { return "HelveticaNeue" }
    }
    enum Copperplate: CopperplateFontProtocol {}
    enum HelveticaNeue: HelveticaNeueFontProtocol {}
}

// MARK: - FontProtocol
protocol FontProtocol {}
private extension FontProtocol {
    static var familyName: String { return "\(self)" }
    static func getFont(style: String, size: CGFloat) -> UIFont { return UIFont(name: familyName + "-" + style, size: size) ?? UIFont(name: familyName, size: size) ?? UIFont.systemFontOfSize(size) }
}

// MARK: - Font Family Protocols
protocol CopperplateFontProtocol: NormalStyleFontProtocol {}
protocol HelveticaNeueFontProtocol: NormalStyleFontProtocol, BoldStyleFontProtocol {}

// MARK: - Font Style Protocols
protocol NormalStyleFontProtocol: FontProtocol {}
extension NormalStyleFontProtocol {
    private static var normal: String { return "" }
    static var Normal: (CGFloat) -> UIFont { return { getFont(normal, size: $0) } }
}
extension NormalStyleFontProtocol where Self: RegularStyleFontProtocol {
    private static var normal: String { return regular }
}

protocol RegularStyleFontProtocol: FontProtocol {}
extension RegularStyleFontProtocol {
    private static var regular: String { return "Regular" }
    static var Regular: (CGFloat) -> UIFont { return { getFont(regular, size: $0) } }
}

protocol BoldStyleFontProtocol: FontProtocol {}
extension BoldStyleFontProtocol {
    private static var bold: String { return "Bold" }
    static var Bold: (CGFloat) -> UIFont { return { getFont(bold, size: $0) } }
}

Fonts.HelveticaNeue(。正常).getFont(15)

enum FontsStyle: String {
    case Normal = ""
    case Regular = "Regular"
    case Bold = "Bold"
    case BoldItalic = "BoldItalic"
}

enum Fonts {
    /// HelveticaNeue
    case Default(FontsStyle)
    /// .Normal | .Bold | .BoldItalic | .CondensedBlack | .CondensedBold | .Italic | .Light | .LightItalic | .Medium | .MediumItalic | .UltraLight | .UltraLightItalic | .Thin | .ThinItalic
    case HelveticaNeue(FontsStyle)

    private var default_: Fonts {
        return HelveticaNeue(.Normal)
    }

    var name: String {
        switch self {
            case .Default:
                return default_.name
            case .HelveticaNeue:
                return "HelveticaNeue"
        }
    }

    var style: FontsStyle {
        switch self {
            case .Default(let style):
                return style
            case .HelveticaNeue(let style):
                return style
        }
    }

    func getFont(size: CGFloat) -> UIFont {
        let name = self.name
        let style = self.style.rawValue
        let font = UIFont(name: name + "-" + style, size: size) ?? UIFont(name: name, size: size) ?? UIFont.systemFontOfSize(size)
        return font
    }
}

0 个答案:

没有答案