如何更改UITextView中第一行的样式?

问题描述 投票:-3回答:2

我想更改UITextView中第一行的样式,因为这是在Apple的Notes应用程序中完成的。他们的第一行具有更大的粗体。如何实现呢?

而且我希望其他行能够像Apple Notes iOS默认应用程序一样动态更改其样式。当我创建新笔记时,第一行的样式是大胆的。但是,当我开始输入时,另一种线型会动态更改为简单。

ios swift uitextview
2个回答
0
投票

正如@danh所评论的,您可以使用UITextView的属性文本质量来执行此操作。只需在界面编辑器中将文本类型设置为“属性”,然后使用以下代码来设置文本。这段代码使用html,但是创建属性文本的方法有多种。

Interface Editor

代码:

    let html = """
    <p><strong><span style="font-size: 48px;">First Line</span></strong></p>
    <p><span style="font-size: 28px;">Other Lines</span></p>
    <p><span style="font-size: 28px;">Other Lines</span></p>
    <p><span style="font-size: 28px;">Other Lines</span></p>
    """

    let data = Data(html.utf8)

    if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {

        textView.attributedText = attributedString

    }

Result


0
投票

为我的问题找到了确切的解决方案-https://hartl.co/2015/05/21/Highlight-first-line.html

© www.soinside.com 2019 - 2024. All rights reserved.