这只是来自WWDC 2015的"引入新系统字体"视频中的Apple示例代码放入游乐场并使用UILabel使用字体功能呈现纯文本分数.[更新为Swift 4]
//: Playground - noun: a place where people can play
import UIKit
import CoreGraphics
let pointSize : CGFloat = 60.0
let systemFontDesc = UIFont.systemFont(ofSize: pointSize,
weight: UIFont.Weight.light).fontDescriptor
let fractionFontDesc = systemFontDesc.addingAttributes(
[
UIFontDescriptor.AttributeName.featureSettings: [
[
UIFontDescriptor.FeatureKey.featureIdentifier: kFractionsType,
UIFontDescriptor.FeatureKey.typeIdentifier: kDiagonalFractionsSelector,
],
]
] )
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
label.font = UIFont(descriptor: fractionFontDesc, size:pointSize)
label.text = "12/48" // note just plain numbers and a regular slash
Run Code Online (Sandbox Code Playgroud)
只需轻拍操场上的眼睛,您就会看到一个美丽的部分.
我不得不在应用程序中做类似的事情。我在普通分数和相关的 unicode 字符之间创建了一个映射,如下所示:
enum Fraction: Double {
case Eighth = 0.125
case Quarter = 0.25
case Third = 0.333333333333333
case Half = 0.5
case TwoThirds = 0.666666666666667
case ThreeQuarters = 0.75
}
func localizedStringFromFraction(fraction: Fraction) -> String {
switch fraction {
case .Eighth:
return NSLocalizedString("\u{215B}", comment: "Fraction - 1/8")
case .Quarter:
return NSLocalizedString("\u{00BC}", comment: "Fraction - 1/4")
case .Third:
return NSLocalizedString("\u{2153}", comment: "Fraction - 1/3")
case .Half:
return NSLocalizedString("\u{00BD}", comment: "Fraction - 1/2")
case .TwoThirds:
return NSLocalizedString("\u{2154}", comment: "Fraction - 2/3")
case .ThreeQuarters:
return NSLocalizedString("\u{00BE}", comment: "Fraction - 3/4")
}
}
Run Code Online (Sandbox Code Playgroud)
如果您需要更多分数的支持,可以在此处找到映射。
| 归档时间: |
|
| 查看次数: |
2106 次 |
| 最近记录: |