どうもはじめです。
今回は文字の装飾をやってみようと思います。
完成図を載せておきます。
fontSizeLabel.font = UIFont(name: "Hiragino Sans", size: 20)
文字サイズを変更したくない場合はsizeに以下を指定することができます。
Label.font.pointSize
nameに指定する文字列は
AttributesInspectorのカスタムフォントのFamilyに表示されている文字列を記載する必要があります。
let KernAttr = [NSKernAttributeName: 20] kernLabel.attributedText = NSMutableAttributedString(string: kernLabel.text!, attributes: KernAttr)
NSKernAttributeNameに渡す値を大きくすれば文字間隔が広がります。
let LineSpaceStyle = NSMutableParagraphStyle() LineSpaceStyle.lineSpacing = 20.0 let lineSpaceAttr = [NSParagraphStyleAttributeName: LineSpaceStyle] lineSpaceLabel.attributedText = NSMutableAttributedString(string: lineSpaceLabel.text!, attributes: lineSpaceAttr)
lineSpacingに渡す値を大きくすれば行の間隔を広げることができます。
let fontColorStyle = [NSForegroundColorAttributeName: UIColor.red] fontColorLabel.attributedText = NSMutableAttributedString(string: fontColorLabel.text!, attributes: fontColorStyle)
UILabelであれば以下でも変更が可能です。
label.textColor = UIColor.blue
let underLineAttrString = NSMutableAttributedString(string: underLineLabel.text!) let underLineAttr = [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue, NSUnderlineColorAttributeName: UIColor.black] as [String : Any] underLineAttrString.addAttributes(underLineAttr, range: NSRange(location: 0,length: (underLineLabel.text?.characters.count)!)) underLineLabel.attributedText = underLineAttrString
下線をひきたい箇所のNSRangeを指定することで指定の箇所のみ下線を引くことができます。
HTML,CSSでテキストリンクを作成するときは<a></a>ですぐにテキストリンクが作成できますが、
Swiftでテキストリンクを実装するとなるとかなりめんどくさいことをしないと実装できないことがわかりました。
みんなこんなことやってるんだ・・・って感じです。
最後まで見ていただきありがとうございました。