LoginSignup
4
3

More than 5 years have passed since last update.

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

Posted at

先日書いた「HTMLファイルを読み込んでUITextLabelに表示」から読み込んだデータをCoreTextを使用して縦組みで表示します。

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 で装飾する
-How do you use NSAttributedString?
-NSAttributedStringを使ってみる
-SwiftでUILabelにHTMLを表示する方法
-ルビを振る

4
3
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
4
3