我的第一个字符中有一个NSMutableAttributedString
+
,我想提升2分。
我可以这样说:
attributedAmountText.addAttribute(NSKernAttributeName, value: 2.0, range: NSRange(location: 0, length: 1))
有没有办法解除第一个角色?有点像垂直kern
编辑:
尝试attributedAmountText.addAttribute(NSBaselineOffsetAttributeName, value: 2.0, range: NSRange(location: 0, length: 1))
实际上会移动基线本身而不是垂直离开基线的文本,这似乎很奇怪,因为文档说不然:
此属性的值是一个NSNumber对象,其中包含一个浮点值,指示角色与基线的偏移量(以磅为单位)。默认值为0.
答案 0 :(得分:0)
嗯......快速测试一下这段代码:
class ScalingViewController: UIViewController {
@IBOutlet weak var theLabel: UILabel!
@IBOutlet weak var theOtherLabel: UILabel!
let now = Date()
let labelA: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = UIColor.cyan
return v
}()
let labelB: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = UIColor.yellow
return v
}()
let labelC: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = UIColor.green
return v
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(labelA)
view.addSubview(labelB)
view.addSubview(labelC)
labelA.topAnchor.constraint(equalTo: view.topAnchor, constant: 40.0).isActive = true
labelA.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8.0).isActive = true
labelB.topAnchor.constraint(equalTo: labelA.topAnchor, constant: 0.0).isActive = true
labelB.leadingAnchor.constraint(equalTo: labelA.trailingAnchor, constant: 8.0).isActive = true
labelC.topAnchor.constraint(equalTo: labelA.topAnchor, constant: 0.0).isActive = true
labelC.leadingAnchor.constraint(equalTo: labelB.trailingAnchor, constant: 8.0).isActive = true
var aText = NSMutableAttributedString()
let baseFont = UIFont.systemFont(ofSize: 30.0, weight: UIFontWeightLight)
let testString = "Example"
let tsLength = testString.characters.count
// Font only
aText = NSMutableAttributedString()
aText.append(NSMutableAttributedString(string: testString, attributes: [NSFontAttributeName: baseFont]))
labelA.attributedText = aText
// Font + positive Baseline Offset of 1st character
aText = NSMutableAttributedString()
aText.append(NSMutableAttributedString(string: testString, attributes: [NSFontAttributeName: baseFont]))
aText.addAttribute(NSBaselineOffsetAttributeName, value: 10.0, range: NSRange(location: 0, length: 1))
labelB.attributedText = aText
// Font + negative Baseline Offset of all but 1st character
aText = NSMutableAttributedString()
aText.append(NSMutableAttributedString(string: testString, attributes: [NSFontAttributeName: baseFont]))
aText.addAttribute(NSBaselineOffsetAttributeName, value: -10.0, range: NSRange(location: 1, length: tsLength - 1))
labelC.attributedText = aText
}
}
给我这个结果:
我想如果我对String属性的所有排版方面有足够的了解,这会有意义吗?