2016-02-16 52 views
0

我有以下重疊的枚舉。將更多數據嵌入enum案例

public enum Icons { 
    public enum Arrow: String { 
     case Angle1 = "\u{f100}" 
     case Angle2 = "\u{f101}" 
     case Angle3 = "\u{f102}" 
     case Angle4 = "\u{f103}" 
     case ArrowBottomLeft = "\u{f104}" 
     case ArrowBottomRight = "\u{f105}" 
    } 

    public enum Clothing: String { 
     case BallCap = "\u{f100}" 
     case Belt = "\u{f101}" 
     case Boot = "\u{f102}" 
     case BowTie = "\u{f103}" 
    } 

    public enum Emotions: String { 
     case Angel = "\u{f100}" 
     case AngrySick = "\u{f101}" 
     case Angry = "\u{f102}" 
     case Bitter = "\u{f103}" 
     case Concerned = "\u{f104}" 
     case Cool = "\u{f105}" 
    } 
} 

我有一個巨大的圖標集合,我想要在我的應用程序中集成。我也有一個UIImage擴展初始值設定項,它將UIFont作爲參數,併爲圖標繪製一個字符串(源自Icons.Category.Icons--請注意,在這種情況下,Category是Arrow,Clothing或Emotions)。

爲了得到一個圖標我打電話:

let image = UIImage( 
         fromIcon: Icons.Emotions.Angel.rawValue, 
         withFont: UIFont.iconFontAngel(22) 
        ) 

這三個類型的圖標具有相關的UIFont擴展:

  • 箭型有UIFont.iconFontArrow(size)
  • 服裝類型有UIFont.iconFontClothing(size)
  • 情緒類型有UIFont.iconFontEmotions(size)

如何更好地聲明圖標以包含UIFont,字體和其他類別特定選項的大小,因爲我確切知道UIFont與每個類別對應的內容,我只需將單個參數(如Icons.Arrow.Angle3)傳遞給UIImage初始值設定項並從此參數中提取字符串,UIFont和其他所需的選項?

我正在考慮宣佈圖標類型設置,但我不知道如何解決這個乾淨的方式。

+0

您可以使用結構與鱗片枚舉和大小值,如果要包括尺寸。現在,您可以使用自省來確定使用哪個UIFont。例如('let icon = Icons.Arrow.Angle3;圖標是Icons.Clothing //將會返回true;而圖標是Icons.Emotions //將返回false') – beyowulf

回答

2

不要爲此使用枚舉。使用一個結構。

public struct FontBasedIcon { 

    private init(string: String, fontName: String) { 
     self.string = string 
     self.fontName = fontName 
    } 

    public let string: String 
    public let fontName: String 

    public func font(size size: CGFloat) -> UIFont { 
     return UIFont(name: fontName, size: size)! 
    } 

    public func image(fontSize fontSize: CGFloat) -> UIImage { 
     let string = self.string as NSString 
     let attributes = [ NSFontAttributeName: self.font(size: fontSize) ] 
     var rect = string.boundingRectWithSize(CGSizeMake(CGFloat.infinity, CGFloat.infinity), options: .UsesLineFragmentOrigin, attributes: attributes, context: nil) 
     let size = CGSizeMake(ceil(rect.width), ceil(rect.height)) 
     UIGraphicsBeginImageContextWithOptions(size, false, 0) 
     defer { UIGraphicsEndImageContext() } 
     string.drawAtPoint(CGPoint.zero, withAttributes: attributes) 
     return UIGraphicsGetImageFromCurrentImageContext() 
    } 

} 

然後宣告您的常量,靜態屬性:

public extension FontBasedIcon { 
    public struct Arrow { 
     public static let Angle1 = FontBasedIcon(string: "\u{f100}", fontName: "ArrowFont") 
     public static let Angle2 = FontBasedIcon(string: "\u{f101}", fontName: "ArrowFont") 
     // etc. 
    } 

    public struct Emotion { 
     public static let Angel = FontBasedIcon(string: "\u{f100}", fontName: "EmotionFont") 
     public static let AngrySick = FontBasedIcon(string: "\u{f101}", fontName: "EmotionFont") 
     // etc. 
    } 
} 

用法:

let angelImage = FontBasedIcon.Emotion.Angel.image(fontSize: 22)