gpt4 book ai didi

swift - 如何在 Swift 2 的 UITextView 中将大型静态文本的某些部分设为粗体或斜体

转载 作者:行者123 更新时间:2023-11-30 10:06:52 26 4
gpt4 key购买 nike

我只有三个月的Swift学习经验,所以请原谅我的无知。

我正在做一个图书阅读应用程序并使用 json 文件填充章节。

我面临的问题是:1. 本章内容之间可以有子标题或引号,这些子标题或引号应为粗体或斜体。我不知道如何识别这些部分并以粗体/斜体显示它们。

我有两种方法来解决这个问题:
1. 仅使用 UITextView 来显示章节内容。在本例中,我的 json 如下所示:

  "book": {
"name": "Fairy Tale,",
"chapters": [
{
"chapterNumber": 1,
"chapterName": "Tale First",
"chapterContents": "<\bold>This is a first heading.And it will be bold.Contents come here<\bold>.This will be plain text. Non bold.<\bold>This is a second heading.<\bold> And it will be bold.Contents come here.This will be plain text. Non bold."
}
]
}

放置一些像<\bold>这样的字符串并用它来分割它。然后以粗体显示标题字符串,以非粗体显示内容字符串。但这看起来不像是一个解决方案,我不确定我是否可以正确地将其拆分并为所有 18 章执行此操作。

  • 使用 UITableViewCell 并将 UITextView 放入其中。并传递一个 ChapterContents 数组及其标题和内容。所以我的 json 看起来像这样。

  • “书”: {
    “名称”:“童话故事”,
    “章节”:[
    {
    “章节号”:1,
    "chapterName": "故事第一",
    “章节内容”:[
    {
    "contentHeader": "这是第一个标题。它会是粗体。",
    "contents": "内容放在这里。这将是纯文本。非粗体。"
    },
    {
    "contentHeader": "这是第二个标题。它会是粗体。",
    "contents": "内容放在这里。这将是纯文本。非粗体。"
    }
    ]
    }
    ]
    }
    然后迭代 ChapterContents 数组,并将每个元素放入一个 TableViewCell 中,其中包含用于显示 contentHeader 的 UILabel 和用于显示内容的 UITextView。但有了这个,我将不得不删除滚动,因为我希望章节滚动屏幕的整个长度,而不是限制单元格高度。我不确定图书阅读应用程序是否应该这样做。

    这两个看起来都不像可行的解决方案。有人可以帮我知道吗
    1.是否有其他组件支持这种显示或我可以实现的不同解决方案来找出粗体和非粗体部分。?
    2. 或者,如果在 UITableViewCell 中使用 UITextView 是实现此目的的方法之一。?

    提前致谢。我正在使用 Swift 2。

    最佳答案

       let json : NSString  = <h1>Heading</h1><p><strong>Lorem Ipsum</strong> 
    is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
    It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <h1>Heading</h1><p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry.
    Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
    It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>


    let attrStr = try! NSAttributedString(
    data: json.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
    options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
    documentAttributes: nil)

    dispatch_async(dispatch_get_main_queue(), {
    yourTextView.attributedText = attrStr
    })

    关于swift - 如何在 Swift 2 的 UITextView 中将大型静态文本的某些部分设为粗体或斜体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35449890/

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