Swift 是否有一种简洁的方式将分数表示为属性字符串?
我想知道是否有一个内置的方法来表示Swift 是否有一种简洁的方式将分数表示为属性字符串?,swift,Swift,我想知道是否有一个内置的方法来表示 var someFraction = "1/12" 作为属性字符串?i、 e.“1”升高并压缩,而“12”降低并压缩 谢谢,您可以使用unicode来显示分数,而不是使用属性字符串。这有一个用objective C编写的源代码。您可以轻松地将其移植到swift中。您可以使用unicode来显示分数,而不是使用属性字符串。这有一个用objective C编写的源代码。您可以轻松地将其移植到swift中。如果您希望正确表示任意分数,对于自定义UIFontDesc
var someFraction = "1/12"
作为属性字符串?i、 e.“1”
升高并压缩,而“12”
降低并压缩
谢谢,您可以使用unicode来显示分数,而不是使用属性字符串。这有一个用objective C编写的源代码。您可以轻松地将其移植到swift中。您可以使用unicode来显示分数,而不是使用属性字符串。这有一个用objective C编写的源代码。您可以轻松地将其移植到swift中。如果您希望正确表示任意分数,对于自定义UIFontDescriptor中的UIFontDescriptorFeatureSettingsAttribute,应分别将
UIFontFeatureTypeIdentifierKey
和UIFontFeatureSelectorIdentifierKey
设置为KfractionType
和kDiagonalFractionsSelector
。例如,你可以说:
let label = UILabel(frame: CGRect(x: 0.0, y: 0.0, width: 1000.0, height: 100.0))
let pointSize : CGFloat = 60.0
let systemFontDesc = UIFont.systemFontOfSize(pointSize,
weight: UIFontWeightLight).fontDescriptor()
let fractionFontDesc = systemFontDesc.fontDescriptorByAddingAttributes(
[
UIFontDescriptorFeatureSettingsAttribute: [
[
UIFontFeatureTypeIdentifierKey: kFractionsType,
UIFontFeatureSelectorIdentifierKey: kDiagonalFractionsSelector,
], ]
] )
label.font = UIFont(descriptor: fractionFontDesc, size:pointSize)
label.text = "The Fraction is: 23/271"
结果如下:
你可以找到更多信息
Swift 3.0
如果要正确表示任意分数,对于自定义UIFontDescriptor中的UIFontDescriptorFeatureSettingsAttribute,应分别将
UIFontFeatureTypeIdentifierKey
和UIFontFeatureSelectorIdentifierKey
设置为KfractionType
和kDiagonalFractionsSelector
。例如,你可以说:
let label = UILabel(frame: CGRect(x: 0.0, y: 0.0, width: 1000.0, height: 100.0))
let pointSize : CGFloat = 60.0
let systemFontDesc = UIFont.systemFontOfSize(pointSize,
weight: UIFontWeightLight).fontDescriptor()
let fractionFontDesc = systemFontDesc.fontDescriptorByAddingAttributes(
[
UIFontDescriptorFeatureSettingsAttribute: [
[
UIFontFeatureTypeIdentifierKey: kFractionsType,
UIFontFeatureSelectorIdentifierKey: kDiagonalFractionsSelector,
], ]
] )
label.font = UIFont(descriptor: fractionFontDesc, size:pointSize)
label.text = "The Fraction is: 23/271"
结果如下:
你可以找到更多信息
Swift 3.0
使用上述
UIFont
扩展名:
static func fractionFont(forTextStyle textStyle: UIFontTextStyle) -> UIFont {
let font = UIFont.preferredFont(forTextStyle: .title1)
let descriptor = font.fontDescriptor.addingAttributes(
[
UIFontDescriptor.AttributeName.featureSettings: [
[
UIFontDescriptor.FeatureKey.featureIdentifier: kFractionsType,
UIFontDescriptor.FeatureKey.typeIdentifier: kDiagonalFractionsSelector,
],
]
] )
return UIFont(descriptor: descriptor, size: font.pointSize)
}
然后在某个地方执行以下函数:
func fractionMutableAttributedString(for string: String) -> NSMutableAttributedString {
let attributes: [NSAttributedStringKey: Any] = [.foregroundColor: UIColor.blue]
let attributedText = NSMutableAttributedString(string: string, attributes: attributes)
let substring = string.split(separator: " ") // Do we have a fractional value?
if substring[1].contains("/") {
let range = (string as NSString).range(of: String(substring[1]))
attributedText.addAttribute(NSAttributedStringKey.font, value: UIFont.fractionFont(forTextStyle: .title1), range: range)
}
return attributedText
}
以及用作:
let string = "3 5/6"
label.attributedText = fractionMutableAttributedString(for: string)
要生成正确的分数。请使用上述
UIFont
扩展名:
static func fractionFont(forTextStyle textStyle: UIFontTextStyle) -> UIFont {
let font = UIFont.preferredFont(forTextStyle: .title1)
let descriptor = font.fontDescriptor.addingAttributes(
[
UIFontDescriptor.AttributeName.featureSettings: [
[
UIFontDescriptor.FeatureKey.featureIdentifier: kFractionsType,
UIFontDescriptor.FeatureKey.typeIdentifier: kDiagonalFractionsSelector,
],
]
] )
return UIFont(descriptor: descriptor, size: font.pointSize)
}
然后在某个地方执行以下函数:
func fractionMutableAttributedString(for string: String) -> NSMutableAttributedString {
let attributes: [NSAttributedStringKey: Any] = [.foregroundColor: UIColor.blue]
let attributedText = NSMutableAttributedString(string: string, attributes: attributes)
let substring = string.split(separator: " ") // Do we have a fractional value?
if substring[1].contains("/") {
let range = (string as NSString).range(of: String(substring[1]))
attributedText.addAttribute(NSAttributedStringKey.font, value: UIFont.fractionFont(forTextStyle: .title1), range: range)
}
return attributedText
}
以及用作:
let string = "3 5/6"
label.attributedText = fractionMutableAttributedString(for: string)
生成正确的分数。谢谢!你知道有没有办法使用这个字体功能来创建一个前面有数字的分数,比如3⅚? 我原以为斜杠两边只有上标和下标数字,但在具有此功能的字体中使用的任何数字都会变成上标。也许有一种方法可以打破某些角色?我不认为这是可能的。一种方法是使用属性字符串。有关示例,请参见我的更新答案。@有关“3 5/6”的一般方法,请参见我的答案。谢谢!你知道有没有办法使用这个字体功能来创建一个前面有数字的分数,比如3⅚? 我原以为斜杠两边只有上标和下标数字,但在具有此功能的字体中使用的任何数字都会变成上标。也许有一种方法可以打破某些角色?我不认为这是可能的。一种方法是使用属性字符串。有关示例,请参见我的更新答案。@有关“3 5/6”的一般方法,请参见我的答案