我只有三个月的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."
}
]
}
放一些字符串,例如&lt; \ bold&gt;并用它来分裂它。然后以粗体显示字符串的标题和非粗体显示内容。但这看起来不像是一个解决方案,我不确定,如果我可以正确地拆分它并且也为所有18章做到这一点。
"book": {
"name": "Fairy Tales,",
"chapters": [
{
"chapterNumber": 1,
"chapterName": "Tale First",
"chapterContents": [
{
"contentHeader": "This is a first heading.And it will be bold.",
"contents": "Contents come here.This will be plain text. Non bold."
},
{
"contentHeader": "This is a second heading.And it will be bold.",
"contents": "Contents come here.This will be plain text. Non bold."
}
]
}
]
}
然后遍历chapterContents数组,并将每一个放在一个TableViewCell中,其中包含用于显示contentHeader的UILabel,以及用于显示内容的UITextView。但有了这个,我将不得不删除滚动,因为我希望章节滚动屏幕的整个长度,而不是限制单元格高度。我不确定这是否应该如何完成图书阅读应用程序。
这些都看起来不太可行。
有人可以帮我知道
1.如果有其他组件支持这种显示或我可以实现的不同解决方案,以找出粗体和非粗体部分。?
2.或者如果在UITableViewCell中使用UITextView是其中一种方法。?
提前致谢。我正在使用Swift 2。
答案 0 :(得分:1)
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
})