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和其他所需的選項?
我正在考慮宣佈圖標類型設置,但我不知道如何解決這個乾淨的方式。
您可以使用結構與鱗片枚舉和大小值,如果要包括尺寸。現在,您可以使用自省來確定使用哪個UIFont。例如('let icon = Icons.Arrow.Angle3;圖標是Icons.Clothing //將會返回true;而圖標是Icons.Emotions //將返回false') – beyowulf