gpt4 book ai didi

ios - NSAttributedString:如何在另一个 NSAttributedString 中制作多行

转载 作者:行者123 更新时间:2023-11-28 07:25:24 24 4
gpt4 key购买 nike

我正在尝试在另一个文本中创建多行单独文本以实现以下文本样式。

enter image description here

我已经尝试使用下面的代码来实现目标,但是代码的第三部分产生了一个问题(中等字体)

enter image description here

    private func createLimitedDetailText() -> NSAttributedString {
let totalText = "Attension, only\n 6 spaces\n left!"
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center

let attributedString = NSMutableAttributedString(string: totalText, attributes: [
.font: FontFamily.OpenSans.light.font(size: 29.0),
.foregroundColor: UIColor.white,
])

let bigText = attributedString.addAttribute(.font, value: FontFamily.OpenSans.extrabold.font(size: 70), range: NSRange(location: 17, length: 1))
let medium = attributedString.addAttribute(.font, value: FontFamily.OpenSans.semibold.font(size: 29), range: NSRange(location: 18, length: 14))

let textRange = NSRange(location: 0, length: attributedString.length)
attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: textRange)

return attributedString
}

最佳答案

如果有人想知道答案,下面的代码会生成完全相同的结果:

    private func createLimitedDetailText() -> NSAttributedString {
let totalText = "Attension, only\n 6"
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = 0.90
paragraphStyle.alignment = .center

let attributedString = NSMutableAttributedString(string: totalText, attributes: [
.font: FontFamily.OpenSans.light.font(size: 29.0),
.foregroundColor: UIColor.white,
NSAttributedString.Key.paragraphStyle: paragraphStyle
])

attributedString.addAttribute(.font, value: FontFamily.OpenSans.extrabold.font(size: 70), range: NSRange(location: 17, length: 1))

let paragraphStyle2 = NSMutableParagraphStyle()
paragraphStyle2.lineHeightMultiple = 0.30
paragraphStyle2.alignment = .center

let attributedString2 = NSMutableAttributedString(string: " spaces\n left!", attributes: [
.font: FontFamily.OpenSans.semibold.font(size: 29.0),
.foregroundColor: UIColor.white,
.baselineOffset: 35,
.paragraphStyle: paragraphStyle2
])
attributedString.append(attributedString2)

return attributedString
}

关于ios - NSAttributedString:如何在另一个 NSAttributedString 中制作多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56756529/

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