[swift] UIViewにhtmlを縦組みで表示


先日書いた「[HTMLファイルを読み込んでUITextLabelに表示](http://qiita.com/norizou4/items/bf2740807794b71f143b)」から読み込んだデータをCoreTextを使用して縦組みで表示します。

````swift:secondViewController.swift
import CoreText
import UIKit

class secondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //UIViewで呼び出します。
        self.view = View()
    }

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

    class View: UIView {
       var htmlText = "" //HTMLのテキスト初期化
        override func drawRect(rect: CGRect) {
            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]
            }
            
            
            var height = 28.0
            var settings = [
                CTParagraphStyleSetting(
                    spec: .MinimumLineHeight,
                    valueSize: UInt(sizeofValue(height)),
                    value: &height)
            ]
            let style = CTParagraphStyleCreate(settings, UInt(settings.count))
        
            
            var err:NSError?
            var nsstringHtml = NSMutableAttributedString(
                data: htmlText.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
                options: [ NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType ],
                documentAttributes: nil,
                error: &err)
            
            let attributedText = NSMutableAttributedString(attributedString: nsstringHtml!)

           attributedText.addAttributes([
                NSFontAttributeName: UIFont(name: "HiraMinProN-W3", size: 14.0)!,
                NSVerticalGlyphFormAttributeName: true,
                kCTParagraphStyleAttributeName: style,
                ],
                range: NSMakeRange(0, attributedText.length))

            let context = UIGraphicsGetCurrentContext()
            
            CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0)
            CGContextAddRect(context, rect)
            CGContextFillPath(context)
            
            CGContextRotateCTM(context, CGFloat(M_PI_2))
            CGContextTranslateCTM(context, 30.0, 35.0)
            CGContextScaleCTM(context, 1.0, -1.0)
            
            let framesetter = CTFramesetterCreateWithAttributedString(attributedText)
            let path = CGPathCreateWithRect(CGRectMake(0.0, 0.0, rect.height, rect.width), nil)
            let frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, nil)
            CTFrameDraw(frame, context)
        }
    }
}
````

しかし、これではすべての文字が同じフォントサイズで、同じフォントになってしまう、、、

-[UILabel, UITextView, UITextField のテキストを NSAttributedString で装飾する](http://qiita.com/shu223/items/c2eb639976154a491d19)
-[How do you use NSAttributedString?](http://stackoverflow.com/questions/3482346/how-do-you-use-nsattributedstring)
-[NSAttributedStringを使ってみる](http://tea-leaves.jp/home/ja/article/1374929836)
-[SwiftでUILabelにHTMLを表示する方法](http://qiita.com/satomikko94/items/31ce11a7b525552d4742)
-[ルビを振る](http://qiita.com/woxtu/items/284369fd2654edac2248)