在哪里放置杂项广泛使用的功能?

时间:2016-02-12 18:40:03

标签: swift

此问题来自Is there a neat way to represent a fraction as an attributed string?

我有一个为分数字符串

提供字体的函数
1|3|5|7|9

我在其中使用func fractionFont() -> UIFont { let pointSize = CGFloat(20.0) let systemFontDescriptor = UIFont.systemFontOfSize(pointSize, weight: UIFontWeightLight).fontDescriptor() let fractionFontDescriptor = systemFontDescriptor.fontDescriptorByAddingAttributes( [ UIFontDescriptorFeatureSettingsAttribute: [ [ UIFontFeatureTypeIdentifierKey: kFractionsType, UIFontFeatureSelectorIdentifierKey: kDiagonalFractionsSelector, ], ] ] ) return UIFont(descriptor: fractionFontDescriptor, size: pointSize) }

中的标签属性
SomeClass

说我想在class SomeClass { @IBOutlet weak var fractionLabel: UILabel! { didSet { fractionLabel.text = "1/2" fractionLabel.font = fractionFont() } } } 中再次使用fractionFont方法我想复制代码并不是一个好主意。我还在另一篇文章中建议我谨慎使用所谓的AnotherClass课程,那么最好的方法是什么呢?

  • 感谢

2 个答案:

答案 0 :(得分:5)

我会使用您的UIFont方法延长fractionFont

extension UIFont {
    class func fractionFont() -> UIFont {
        /* Code here */
    }
}

答案 1 :(得分:0)

创建类似Misc的类 将您的函数粘贴为类func。 现在您可以像fractionLabel.font = Misc.fractionFont()

一样调用它
相关问题