LoginSignup
10
9

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-02-22

画面を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を表示する方法

10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9