Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active July 1, 2016 23:28
Show Gist options
  • Save KentarouKanno/47844cf7d929114ebb8814fb9f9dd69d to your computer and use it in GitHub Desktop.
Save KentarouKanno/47844cf7d929114ebb8814fb9f9dd69d to your computer and use it in GitHub Desktop.
NSMutableAttributedString

NSMutableAttributedString

★ 文字間のカーニングを調整する

let myLabel: UILabel = UILabel(frame: CGRectMake(0, 100, 200, 50))
myLabel.text = "Hello Swift!!"

let attributedText = NSMutableAttributedString(string:myLabel.text!)
let customLetterSpacing = 10
attributedText.addAttribute(NSKernAttributeName, value: customLetterSpacing, range: NSMakeRange(0, attributedText.length))
myLabel.attributedText = attributedText
myLabel.sizeToFit()
view.addSubview(myLabel)

image

★ 枠付きの文字を作成する

var text = "ABC,abc,123"

var attributedString = NSMutableAttributedString(string:text,attributes: [NSFontAttributeName:UIFont(name:"Georgia", size:17.0)!])
attributedString.addAttribute(NSStrokeWidthAttributeName, value: 3.0,range: NSMakeRange(0, text.characters.count ?? 0))
label.attributedText = attributedString

image

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