2017-01-17 1 views
0

Das ist mein HTML-String zu machen:Wie HTML-String in UITextView mit unterschiedlichen Schriftstilen, aber gleiche Schriftfamilie

<i>Italics </i><b>Bold </b><u>Underline</u><blockquote>block quote<br></blockquote> 

Ich möchte alle Styling sein gleichen (kursiv, fett usw.), aber ich möchte Implementieren Sie die Schriftfamilie als Open Sans. Also, wo immer es kursiv ist, möchte ich, dass die Schriftart Open-Sans Italics ist. Wie implementiere ich das? Bitte helfen Sie.

+0

ich versuchen würde, einen 'NSAttributedString' mit der' NSHTMLTextDocumentType' Option zu konstruieren und ändern dann die Attribute die gewünschte Schriftart auf jeder seinen Teil zu erzwingen. – werediver

Antwort

1

versuchen Sie es mit NSAttributedString mit NSHTMLTextDocumentType Option und .utf8 String-Codierung

Swift 3:

extension String { 

    var htmlAttributedString: NSAttributedString? { 

     do { 
      return try NSAttributedString(data: data(using: .utf8)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil) 
     } catch let error { 
      print(error.localizedDescription) 
     } 
     return nil 
    } 
} 

Verwenden

let htmlStringCode = "<i>Italics </i><b>Bold </b><u>Underline</u><blockquote>block quote<br></blockquote>" 

if let htmlText = htmlStringCode.htmlAttributedString { 
     let attributes = [NSFontAttributeName: UIFont(name: "Open Sans", size: 18.0)!] 
     let attributedText = NSMutableAttributedString(string: htmlText.string, attributes: attributes) 

     let htmlString = htmlText.string as NSString 
     let range = htmlString.range(of: "Italics") 
     attributedText.addAttribute(NSFontAttributeName, value: UIFont(name: "OpenSans-Italic", size: 18.0)!, range: range) 

     textView.attributedText = attributedText 
} 
+0

So würde es nur die Systemschriftart und nicht meine benutzerdefinierte Schriftart – Ghazalah

+0

Sie können bearbeiteter Code versuchen und verwenden Sie die gleiche Logik, wo Sie benutzerdefinierte Schriftart-Attribute benötigen – suhit

0

Ein Weg, um das gewünschte Verhalten zu erreichen, ist diese (kopieren -and-Paste auf einen Spielplatz).

import PlaygroundSupport 
import UIKit 

func attributedString(fromHTML html: String, fontFamily: String) -> NSAttributedString? { 
    let prefix = "<style>* { font-family: \(fontFamily) }</style>" 
    return (prefix + html).data(using: .utf8).map { 
     try! NSMutableAttributedString(
      data: $0, 
      options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], 
      documentAttributes: nil 
     ) 
    } 
} 

let attributedText = attributedString(fromHTML: "<i>Italics </i><b>Bold </b><u>Underline</u><blockquote>block quote<br></blockquote>", fontFamily: "Courier") 

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50)) 
label.backgroundColor = .white 
label.attributedText = attributedText 
PlaygroundPage.current.liveView = label 

Example

1

hatte ich meine eigene CSS hinzuzufügen. Dies löste mein Problem:

UIFont *postFont = [UIFont fontWithName:@"OpenSans" size:13.0]; 

NSString *postWithFont = [self.msgHtml stringByAppendingString:[NSString stringWithFormat:@"<style>body{font-family: '%@'; font-size:%fpx;}i{font-family: '%@'; font-size:%fpx;}</style>", postFont.fontName, postFont.pointSize, [postFont.fontName stringByAppendingString:@"-Italic"], postFont.pointSize]]; 

_mainText = [[NSMutableAttributedString alloc] 
             initWithData:[postWithFont dataUsingEncoding:NSUTF8StringEncoding] 
             options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, 
               NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
             documentAttributes:nil 
             error:nil]; 
+0

Ein separater Stil für '' scheint mir überflüssig. Ein Stil für '' oder '*' (alles) funktioniert (wie in [meine Antwort] (http://stackoverflow.com/a/41694316/3541063) gezeigt). – werediver

Verwandte Themen