Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active December 28, 2018 01:20
Show Gist options
  • Save KentarouKanno/50d76fc4ff7eaf0810b48809a8d4accd to your computer and use it in GitHub Desktop.
Save KentarouKanno/50d76fc4ff7eaf0810b48809a8d4accd to your computer and use it in GitHub Desktop.

ラベルのAttributedStringを再設定する

※ Storyboardで設定したAttributedStringが反映されないことがある為再度コードにて設定し直す

extension UILabel {
    /// Storyboardから設定したNSAttributedStringがUIに反映されない事がある為コードにて再度設定する処理
    func resettingAttributes(text: String) {
        if let attributedString = self.attributedText,
            !attributedString.string.isEmpty {
            
            let resettingAttributes = attributedString.attributes(at: 0, effectiveRange: nil)
            self.attributedText = NSAttributedString(string: text, attributes: resettingAttributes)
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment