LoginSignup
3
2

More than 5 years have passed since last update.

NSAttributeStringをメソッドチェーンで使う

Posted at

目的

NSAttributedStringを簡単に書きたい!
視認性もあげたい!
メソッドチェーンってSwiftっぽい!(

全てのAttrbuteNameはカバーしていないので、必要なものを同様の書式で書き足せば大丈夫です。

動作確認環境

  • Xcode 9.0.1
  • Swift 3.2

使い方

sample.swift

// 基本
let str1 = "サンプル".mutableAttributed
.font(UIFont.systemFont(ofSize: 14))
.letterSpacing(1)

// NSAttributedStringからMutableへ変換して使用
let str2 = "ほげほげ".attributed
str.mutable
.text(color: UIColor.red)
.font(UIFont.systemFont(ofSize: 14))
.letterSpacing(1)

// 2つを結合してParagraphStyleを設定したり
let style = NSMutableParagraphStyle()
.lineSpacing(space: 2)
.textAlignment(.center)

(str1 + str2).paragraphStyle(style)

さらにNSAttributedStringの配列をjoinedで結合したいも利用すると色々便利!(宣伝

実装

NSAttributedString+extension.swift

public extension String {

    public var attributed: NSAttributedString {
        return NSAttributedString(string: self)
    }

    public var mutableAttributed: NSMutableAttributedString {
        return NSMutableAttributedString(string: self)
    }
}

public extension NSAttributedString {

    public var mutable: NSMutableAttributedString {
        return NSMutableAttributedString(attributedString: self)
    }
}

public extension NSMutableAttributedString {

    public func text(color: UIColor) -> Self {
        return text(color: color, range: NSRange(location: 0, length: self.length))
    }

    public func text(color: UIColor, range: NSRange) -> Self {
        addAttribute(NSForegroundColorAttributeName, value: color, range: range)
        return self
    }

    public func font(_ f: UIFont) -> Self {
        return font(f, range: NSRange(location: 0, length: self.length))
    }

    public func font(_ f: UIFont, range: NSRange) -> Self {
        addAttribute(NSFontAttributeName, value: f, range: range)
        return self
    }

    public func letterSpacing(_ spacing: CGFloat) -> Self {
        return letterSpacing(spacing, range: NSRange(location: 0, length: self.length))
    }

    public func letterSpacing(_ spacing: CGFloat, range: NSRange) -> Self {
        addAttribute(NSKernAttributeName, value: spacing, range: range)
        return self
    }

    public func paragraphStyle(_ style: NSParagraphStyle) -> Self {
        return paragraphStyle(style, range: NSRange(location: 0, length: self.length))
    }

    public func paragraphStyle(_ style: NSParagraphStyle, range: NSRange) -> Self {
        addAttribute(NSParagraphStyleAttributeName, value: style, range: range)
        return self
    }
}

public extension NSMutableParagraphStyle {

    public func minimumLineHeight(_ min: CGFloat) -> Self {
        minimumLineHeight = min
        return self
    }

    public func maximumLineHeight(_ max: CGFloat) -> Self {
        maximumLineHeight = max
        return self
    }

    public func lineHeightMultiple(_ multiple: CGFloat) -> Self {
        lineHeightMultiple = multiple
        return self
    }

    public func firstLineHeadIndent(_ indent: CGFloat) -> Self {
        firstLineHeadIndent = indent
        return self
    }

    public func textAlignment(_ alignment: NSTextAlignment) -> Self {
        self.alignment = alignment
        return self
    }

    public func lineBreakMode(_ mode: NSLineBreakMode) -> Self {
        lineBreakMode = mode
        return self
    }

    public func lineSpacing(_ space: CGFloat) -> Self {
        lineSpacing = space
        return self
    }
}

public func + (lhs: NSMutableAttributedString, rhs: NSAttributedString) -> NSMutableAttributedString {
    lhs.append(rhs)
    return lhs
}

public func + (lhs: NSAttributedString, rhs: NSAttributedString) -> NSAttributedString {
    let str = NSMutableAttributedString(attributedString: lhs)
    str.append(rhs)
    return str
}

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