Last active
October 19, 2018 07:03
-
-
Save osanaikoutarou/b1a630982017434c659624e79b0484b5 to your computer and use it in GitHub Desktop.
LineSpacingLabel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LineSpacingLabel.swift | |
// SimplePhotoMemo | |
// | |
// Created by osanai on 2018/10/09. | |
// Copyright © 2018年 osanai. All rights reserved. | |
// | |
// 行間をStoryboard上で調整する | |
import UIKit | |
class LineSpacingLabel: UILabel { | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
setLineSpacingValue(value: self.lineSpace) | |
} | |
@IBInspectable var lineSpace:CGFloat = 0 { | |
didSet { | |
self.setLineSpacingValue(value: lineSpace) | |
} | |
} | |
override var text:String? { | |
didSet { | |
self.setLineSpacingValue(value: self.lineSpace) | |
} | |
} | |
func set(text:String, lineSpace:CGFloat) { | |
self.text = text | |
setLineSpacingValue(value: lineSpace) | |
} | |
func setLineSpacingValue(value:CGFloat) { | |
let attrString = NSMutableAttributedString(string: self.text!) | |
let style = NSMutableParagraphStyle() | |
style.lineSpacing = value | |
style.lineBreakMode = self.lineBreakMode | |
attrString.addAttribute(.paragraphStyle, | |
value: style, | |
range: NSMakeRange(0, attrString.length)) | |
self.attributedText = attrString | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment