这就是我想要实现的目标。
我用了attributedString
,获取字符 $ 的范围并对其应用属性,如下所示:
let str = "$4"
let r1 = str.range(of: "$")!
let n1 = NSRange(r1, in: str)
let atrStr = NSMutableAttributedString(string: str)
atrStr.addAttributes([NSAttributedStringKey.font : UIFont.systemFont(ofSize: 25)], range: n1)
atrStr.addAttributes([NSAttributedStringKey.foregroundColor : UIColor.lightGray], range: n1)
lbl.attributedText = atrStr
但是结果是
如何从下到上更改该特定字符的对齐方式?
最佳答案
您需要使用NSMutableAttributedString
的baselineOffset
计算两种字体大小之间的差异
let offset = baseFont.capHeight - smallFont.capHeight
添加名为baselineOffset的新属性
atrStr.addAttributes([NSAttributedString.Key.baselineOffset:offset], range: n1)
关于ios - 如何更改属性字符串中字符的对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49509851/