Help us understand the problem. What is going on with this article?

[swift] HTMLファイルを読み込んでUITextLabelに表示

More than 3 years have passed since last update.

画面を2つ作って「ViewController」と「secondViewController」とします。
viewControllerにダウンロードのボタンと開くボタンを設置して、ダウンロードボタンでHTMLを読み込んで保存し、開くボタンで画面遷移させ、scondViewControllerで表示させます。

ViewController.swift
import UIKit

class ViewController: UIViewController {
    //ダウンロードボタン
    @IBAction func Download(sender: AnyObject) {

        //青空文庫から阿呆の一生をダウンロードURL
        let newsUrlString = "http://www.aozora.gr.jp/cards/000879/files/19_14618.html"

        var url = NSURL(string: newsUrlString)!
        var request = NSURLRequest(URL: url)

        //データをダウンロードする
        var data = NSURLConnection.sendSynchronousRequest(request, returningResponse:nil, error:nil)
        var htmlString = NSString(data:data!, encoding:NSShiftJISStringEncoding)!

        //パスの取得
        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as Array<String>

        //保存するファイルの名前
        let filePath = String(paths[0]) + "data.html"

        //保存するデータ
        let array = [htmlString]
        //.componentsJoinedByString("\n")

        //アーカイブしてdata.datというファイル名で保存する
        let successful = NSKeyedArchiver.archiveRootObject(array, toFile: filePath)        
    }

    //viewControllerのデフォルト    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

二つ目の画面です。UITextLabelを設置させます。

secondviewController.swift
import UIKit

class secondViewController: UIViewController {

    @IBOutlet weak var testLabel: UITextField!
    var htmlText = "" //HTMLのテキスト
    override func viewDidLoad() {
        super.viewDidLoad()

        //保存したファイルの読み込み
        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
            .UserDomainMask, true) as Array<String>
        let filePath = String(paths[0]) + "data.html"
        if let array = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? Array<String>{
            htmlText = array[0]
        }

        //読み込んだHTMLをUITextLableに表示
        var err:NSError?
        self.testLabel.attributedText = NSAttributedString(
            data: htmlText.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
            options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType],
            documentAttributes: nil,
            error: &err)
        self.testLabel.font = UIFont(name: "System", size: 14)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

参照:SwiftでUILabelにHTMLを表示する方法

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away