简体   繁体   中英

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.

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.

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. That's what was done for the FAQs (which uses a label), I neglected to mention I tried that too.

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). 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. A possible solution would be using a WebView. Converting your "Terms and Conditions" and "FAQ" to HTML would probably be much easier than using an AttributedString.

If you still want to use your HTML in a UITextView you can try this function:

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. 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.

This function returns nil if either the .html file does not exist or the NSAttributedString conversion failed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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