简体   繁体   English

Swift:如何在没有属性字符串的情况下在TextView中显示粗体字

[英]Swift: how to display bold words in textview without attributed string

We're adding the finishing touches to an app we're working on, and that apparently means putting an entire "Terms and Conditions" and "FAQs" section, formatting, bullets, breaks and all. 我们正在为正在开发的应用程序添加画龙点睛的功能,这显然意味着要放置完整的“条款和条件”和“常见问题解答”部分,以及格式,项目符号,中断和所有内容。

So I tried copy-pasting it into a textView with "editable" set to off, which kept the bullets, but not the bolded text. 因此,我尝试将其复制粘贴到关闭了“可编辑”的textView中,以保留项目符号,但不保留粗体文本。

Now, I've done attributed string before, and I have to say, I'm not sure it will be easy to do that on some 12-pages worth of paragraphs, bulleted lists and breaks that are likely to change in a few years or so. 现在,我之前已经完成了属性字符串的使用,我不得不说,我不确定在几年后可能会发生变化的价值约12页的段落,项目符号列表和中断中这样做是否容易或者。

So my question is, is there a way to do this without using attributed string? 所以我的问题是,有没有一种方法可以不使用属性字符串呢?

Barring that, perhaps there's a way to loop through the text, and look for a written tag that will apply the attributes? 除此以外,也许有一种方法可以遍历文本并寻找将应用属性的书面标记?

EDIT: 编辑:

Update. 更新。 It's been suggested I use HTML tags, and web view. 建议使用HTML标记和Web视图。 That's what was done for the FAQs (which uses a label), I neglected to mention I tried that too. 这是针对FAQ(使用标签)所做的,我忽略了我也尝试过的做法。

For some reason, it just shows a blank textview, albeit a large-sized one, as if there's text in it (there isn't any). 由于某种原因,它只是显示一个空白的textview,尽管是一个大的textview,好像其中有文本(没有任何文本)。 Strange that copy-pasting works but this doesn't. 奇怪的是,粘贴粘贴有效,但事实并非如此。

Here's my code for it: 这是我的代码:

override func awakeFromNib() {
    super.awakeFromNib()

    termsTitle.text = "Terms and Conditions"

    htmlContent = "<p style=\"font-family:Helvetica Neue\"><br/><strong><br/> BLA BLA BLA BLA BLA BLA x 12 Pages"

    do {
        let str = try NSAttributedString(data: htmlContent.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
        termsTextView.attributedText = str
    } catch {
        print("Dim background error")
    }
}

I'm pretty sure you can't do this in a Textview without using AttributedString. 我敢肯定,如果不使用AttributedString,则无法在Textview中执行此操作。 A possible solution would be using a WebView. 一种可能的解决方案是使用WebView。 Converting your "Terms and Conditions" and "FAQ" to HTML would probably be much easier than using an AttributedString. 将“条款和条件”和“ FAQ”转换为HTML可能比使用AttributedString容易得多。

If you still want to use your HTML in a UITextView you can try this function: 如果仍要在UITextView中使用HTML,则可以尝试以下功能:

func getAttributedString(fileName: String) -> NSAttributedString? {
   if let htmlLocation = NSBundle.mainBundle().URLForResource(fileName, withExtension: "html"), data = NSData(contentsOfURL: htmlLocation) {
      do {
         let attrString = try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil)
         return attrString
      } catch let err as NSError {
         print("Attributed String Creation Error")
         print(err.localizedDescription)
         return nil
      }
   } else {
      return nil
   }
}

This function assumes you have a .html file in your main bundle. 此函数假定您的主捆绑包中有一个.html文件。 You pass it the name (minus extension) of the file (that should be in your project) and then use it like so: 您将文件(应该在项目中)的名称(负扩展名)传递给它,然后像这样使用它:

textView.attributedText =  getAttributedString("TermsAndConditions")

Just to clarify, the textView is a @IBOutlet on a View Controller in this example. 为了清楚@IBOutlet ,在此示例中, @IBOutlet是视图控制器上的@IBOutlet

This function returns nil if either the .html file does not exist or the NSAttributedString conversion failed. 如果.html文件不存在或NSAttributedString转换失败,则此函数返回nil。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM