しるてく

技術的な話をします

storyboard で UILabel を袋文字にする

袋文字を使いたい病になった。

UIStrokeLabelみたいな感じでクラス作っておく。

import Foundation
import UIKit

@IBDesignable
class UIDecorationLabel: UILabel {
    @IBInspectable var strokeSize: CGFloat = 0
    @IBInspectable var strokeColor: UIColor = UIColor.clearColor()
    
    override func drawTextInRect(rect: CGRect) {
        // stroke
        let cr = UIGraphicsGetCurrentContext()
        let textColor = self.textColor
        
        CGContextSetLineWidth(cr, self.strokeSize)
        CGContextSetLineJoin(cr, kCGLineJoinRound)
        CGContextSetTextDrawingMode(cr, kCGTextStroke)
        self.textColor = self.strokeColor
        super.drawTextInRect(rect)
        
        CGContextSetTextDrawingMode(cr, kCGTextFill)
        self.textColor = textColor
        super.drawTextInRect(rect)
    }
}

あとはUILabelのクラスに上のやつ設定して、適当に値を突っ込んであげればOK