gpt4 book ai didi

ios - Swift 文本的中心和粗体部分

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:00:53 25 4
gpt4 key购买 nike

我想将段落标题居中并加粗。到目前为止,我可以将标题设为粗体,但无法将其居中。

这是我的代码:

    let title = "Title of Paragraph"

let attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15)]

let boldString = NSMutableAttributedString(string:title, attributes:attrs)

let normalText = "Something here.............."

let attributedString = NSMutableAttributedString(string:normalText)

boldString.appendAttributedString(attributedString)

label.attributedText = boldString

我试图在 attrs 中添加另一个属性:

    let attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(15), NSTextAlignment: NSTextAlignment.Center]

我不确定这是否是将它居中的正确方法,但它仍然给出错误“没有更多上下文的表达式类型不明确”

我已经搜索了错误,但似乎还是无法解决问题

最佳答案

我认为问题出在您试图添加到字典中的 NSTextAlignment 键上。它的类型是 Int,它与前一个 String 键变得不明确,所以我想这就是编译器提示的原因。无论哪种方式,NSTextAlignment 都不是用于 NSMutableAttributedString 属性初始化器的有效键。

也许您正在寻找这样的东西:

let string = "Any String"
let style = NSMutableParagraphStyle()
style.alignment = .Center

let attributes = [
NSFontAttributeName: UIFont.boldSystemFontOfSize(15),
NSParagraphStyleAttributeName: style
]

let attributedString = NSMutableAttributedString(string: string, attributes: attributes)

关于ios - Swift 文本的中心和粗体部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34915121/

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