Swift 2 如何在 UITextView 中将大型静态文本的某些部分设为粗体或斜体
How to make certain part of a large static text bold or italic in UITextView in Swift 2
我刚学了三个月Swift,还请见谅
我正在做一个图书阅读应用程序并使用 json 文件填充章节。
我面临的问题是:
1. 本章内容可以有sub headers 或中间的引号,应该是粗体或者一些是斜体。我不确定如何识别这些部分并将它们显示在 bold/italics.
我有两种方法来解决这个问题:
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 看起来像这样。
"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.
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,还请见谅
我正在做一个图书阅读应用程序并使用 json 文件填充章节。
我面临的问题是: 1. 本章内容可以有sub headers 或中间的引号,应该是粗体或者一些是斜体。我不确定如何识别这些部分并将它们显示在 bold/italics.
我有两种方法来解决这个问题:
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 看起来像这样。
"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.
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
})