gpt4 book ai didi

ios - 调整 NSMutableAttributedString 中字符的垂直高度

转载 作者:行者123 更新时间:2023-11-30 12:00:48 24 4
gpt4 key购买 nike

我有一个NSMutableAttributedString+在我想要提升 2 分的第一个角色中。

我可以这样调整字距: attributedAmountText.addAttribute(NSKernAttributeName, value: 2.0, range: NSRange(location: 0, length: 1))

有办法提升第一个字符吗?有点像垂直字距

编辑:

尝试attributedAmountText.addAttribute(NSBaselineOffsetAttributeName, value: 2.0, range: NSRange(location: 0, length: 1))实际上移动基线本身,而不是垂直移动文本离开基线,这看起来很奇怪,因为文档另有说明:

The value of this attribute is an NSNumber object containing a floating point value indicating the character’s offset from the baseline, in points. The default value is 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

}

}

给了我这个结果:

enter image description here

我想如果我对字符串属性的所有排版方面有足够的了解,这就会有意义吗?

关于ios - 调整 NSMutableAttributedString 中字符的垂直高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47267782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com