代码基于Swift4.0
与XCode9
调整UILabel文字的行间距和字间距
如果你也需要调整UILabel的行间距和字间距,那么看这篇总结就对了!!!
UILabel
有一个属性attributedText
open class UILabel : UIView, NSCoding, UIContentSizeCategoryAdjusting {
open var text: String? // default is nil
open var font: UIFont! // default is nil (system font 17 plain)
open var textColor: UIColor! // default is nil (text draws black)
open var shadowColor: UIColor? // default is nil (no shadow)
open var shadowOffset: CGSize // default is CGSizeMake(0, -1) -- a top shadow
open var textAlignment: NSTextAlignment // default is NSTextAlignmentNatural (before iOS 9, the default was NSTextAlignmentLeft)
open var lineBreakMode: NSLineBreakMode // default is NSLineBreakByTruncatingTail. used for single and multiple lines of text
// the underlying attributed string drawn by the label, if set, the label ignores the properties above.
@available(iOS 6.0, *)
@NSCopying open var attributedText: NSAttributedString? // default is nil
}
由注释可知,如果我们设置了attributedText
属性那么label
会忽略以上属性,我们通过设置属性attributedText
实现label
行间距和文字的字间距
attributedText
的类型是NSAttributedString
,因为我们需要自定义属性的值,故创建NSAttributedString
的子类NSMutableAttributedString
guard let content = label.text else {return}
let attributedString : NSMutableAttributedString = NSMutableAttributedString(string: content)
创建了NSMutableAttributedString
的实例attributedString
,接下来我们需要给实例添加值
open func addAttribute(_ name: NSAttributedStringKey, value: Any, range: NSRange)
根据以上方法我们发现值的类型是NSAttributedStringKey
通过结构体NSAttributedStringKey
的属性kern
、paragraphStyle
分别实现文字的间距与行间距
func changeLabelRowSpace(lineSpace: CGFloat, wordSpace: CGFloat) {
guard let content = label.text else {return}
let attributedString : NSMutableAttributedString = NSMutableAttributedString(string: content)
let paragraphStyle : NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpace
attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, (content.count)))
attributedString.addAttribute(NSAttributedStringKey.kern, value: wordSpace, range: NSMakeRange(0, (content.count)))
label.attributedText = attributedString
label.sizeToFit()
}
设置UILabel显示的文字个数
func specifiesTheNumberOfWordsToDisplay(number: Int) {
guard let content = label.text else {return}
let attributedString : NSMutableAttributedString = NSMutableAttributedString(string: content)
if content.count < number {return}
attributedString.deleteCharacters(in: NSMakeRange(number, content.count - number))
label.attributedText = attributedString
label.sizeToFit()
}
使用示例
- 导入
UILabel-Extension
到项目中
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let frame = CGRect(x: 30, y: 130, width: UIScreen.main.bounds.width - 60, height: 400)
let label = UILabel(frame: frame)
label.numberOfLines = 0
label.text = "上海优土视真文化传媒有限公上海浦东张江上海优土视真文化传媒有限公UtoVR上海浦东张江上海优土视真文化传媒有限公UtoVR上海浦东张江上海优土视真文化传媒有限公UtoVR上海浦东张江上海优土视真文化传媒有限公UtoVR上海浦东张江ShenZhengFang优土Brian"
view.addSubview(label)
label.sf.changeLabelRowSpace(lineSpace: 10, wordSpace: 10)
label.sf.specifiesTheNumberOfWordsToDisplay(number: 700)
}
}
源码链接