2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOS13でUILabelのOutlineの色が正しく表示されなくなった話

Last updated at Posted at 2019-09-10

環境

  • iPhone8
  • iOS13.1 β2

原因

  • self.textColorへの設定では反応しなくなった
  • Storyboardで色の指定がデフォルトだとライト、ダークモードに適した文字色になる

iOS12までで動いていたコード

  • UILabelをOverrideしたOutlineLabelを作っていた
before.swift
public class OutlineLabel: UILabel {

    // MARK: - Public Properties
    public var outlineColor: UIColor = .white {
        didSet {
            setNeedsDisplay()
        }
    }
    public var outlineSize: CGFloat = 2 {
        didSet {
            setNeedsDisplay()
        }
    }

    override public func drawText(in rect: CGRect) {
        guard let attrString = attributedText else { return }
        guard let context = UIGraphicsGetCurrentContext() else { return }
        let orgColor = textColor

        context.saveGState()
        context.setLineWidth(outlineSize)
        context.setLineJoin(.round)
        context.setTextDrawingMode(.stroke)
        textColor = outlineColor
        attrString.draw(in: rect)
        context.restoreGState()

        context.saveGState()
        textColor = orgColor
        context.setTextDrawingMode(.fill)
        attrString.draw(in: rect)
        context.restoreGState()
    }
}

iOS13で動くようになったコード

  • 文字色の指定はaddAttributesで行うようにする
after.swift
public class OutlineLabel: UILabel {

    // MARK: - Public Properties
    public var outlineColor: UIColor = .white {
        didSet {
            setNeedsDisplay()
        }
    }
    public var outlineSize: CGFloat = 2 {
        didSet {
            setNeedsDisplay()
        }
    }

    override public func drawText(in rect: CGRect) {
        guard let attrStringBase = attributedText else { return }
        guard let context = UIGraphicsGetCurrentContext() else { return }
        guard let orgColor = textColor else { return }
        
        if #available(iOS 13, *) {
            let attrString = NSMutableAttributedString(attributedString: attrStringBase)
            attrString.addAttributes (
                [NSAttributedString.Key.foregroundColor: self.outlineColor
                ], range: NSRange(location: 0, length: attrString.length))

            context.saveGState()
            context.setLineWidth(outlineSize)
            context.setLineJoin(.round)
            context.setTextDrawingMode(.stroke)
            attrString.draw(in: rect)
            context.restoreGState()

            attrString.addAttributes (
                [NSAttributedString.Key.foregroundColor: orgColor
                ], range: NSRange(location: 0, length: attrString.length))
            context.saveGState()
            context.setTextDrawingMode(.fill)
            attrString.draw(in: rect)
            context.restoreGState()
        } else {
            let attrString = attrStringBase

            textColor = outlineColor
            context.saveGState()
            context.setLineWidth(outlineSize)
            context.setLineJoin(.round)
            context.setTextDrawingMode(.stroke)
            attrString.draw(in: rect)
            context.restoreGState()

            context.saveGState()
            textColor = orgColor
            context.setTextDrawingMode(.fill)
            attrString.draw(in: rect)
            context.restoreGState()
        }
    }
    }
}

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?