Help us understand the problem. What is going on with this article?

AttributedTextを簡単に書けるExtension

UILabelとかUITextViewattributedTextはそのまま書くと結構めんどくさいので楽に書けるextensionを作りました。

もっとこうしたら良いんじゃないのとかダメじゃんて部分あったらコメントで教えてもらえるとありがたいです。

最初に、どう書けるかを書いときます

使い方

let atrStr1 = "aaa".attribute()
    .font(.boldSystemFont(ofSize: 12))
    .textColor(.blue)
    .spacing(5)

let atrStr2 = "bbb".attribute()
    .font(.systemFont(ofSize: 15))
    .textColor(.red)
    .spacing(1)

let atrStr3 = "ccc".attribute()
    .font(.systemFont(ofSize: 12))
    .textColor(.green)
    .spacing(10)

label.attributedText = atrStr1 + atrStr2 + atrStr3

Screen Shot 2019-03-31 at 20.32.41.png

実装法

extension String {

    func attribute() -> NSMutableAttributedString {
        return NSMutableAttributedString(string: self)
    }
}

extension NSMutableAttributedString {

    static func + (left: NSMutableAttributedString, right: NSMutableAttributedString) -> NSMutableAttributedString {
        left.append(right)
        return left
    }

    func font(_ font: UIFont) -> Self {
        addAttribute(.font, value: font, range: NSMakeRange(0, string.count))
        return self
    }

    func textColor(_ color: UIColor) -> Self {
        addAttribute(.foregroundColor, value: color, range: NSMakeRange(0, string.count))
        return self
    }

    func backgroundColor(_ color: UIColor) -> Self {
        addAttribute(.backgroundColor, value: color, range: NSMakeRange(0, string.count))
        return self
    }

    func baseLineOffset(_ offset: CGFloat) -> Self {
        addAttribute(.baselineOffset, value: offset, range: NSMakeRange(0, string.count))
        return self
    }

    func underline(color: UIColor, style: NSUnderlineStyle) -> Self {
        let attributes: [NSAttributedString.Key : Any] = [
            NSAttributedString.Key.underlineStyle : style.rawValue,
            NSAttributedString.Key.underlineColor : color
        ]
        addAttributes(attributes, range: NSMakeRange(0, string.count))
        return self
    }

    func spacing(_ spacing: CGFloat) -> Self {
        addAttribute(.kern, value: spacing, range: NSMakeRange(0, string.count))
        return self
    }
}

個人的に演算子のオーバーロードのところとかが気になっています

参考
https://qiita.com/roba4coding/items/7a5decc7e4f6137fcc88

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした