Skip to content

Instantly share code, notes, and snippets.

@ochim
Created November 15, 2019 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochim/5c262d6c8f3e2c3b4afe98eb523d797f to your computer and use it in GitHub Desktop.
Save ochim/5c262d6c8f3e2c3b4afe98eb523d797f to your computer and use it in GitHub Desktop.
[ios]テキストを下寄せかつ、複数のフォントを混ぜて表示する
//  下寄せ
valueField.contentVerticalAlignment = .bottom

let stringAttributes1: [NSAttributedString.Key: Any] = [
    .foregroundColor: UIColor.black,
    .font: UIFont.systemFont(ofSize: 30.0)
]
let string1 = NSAttributedString(string: v, attributes: stringAttributes1)

let stringAttributes2: [NSAttributedString.Key: Any] = [
    .foregroundColor: UIColor.black,
    .font: UIFont.systemFont(ofSize: 17.0)
]
let string2 = NSAttributedString(string: unit, attributes: stringAttributes2)

valueField.attributedText = NSMutableAttributedString.create(strings: [string1, string2])

extension NSMutableAttributedString {

    static func create(strings: [NSAttributedString]) -> NSMutableAttributedString {
        let mas = NSMutableAttributedString()
        strings.forEach({ string  in mas.append(string) })
        return mas
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment