|
//
// UILabelExt.swift
// ExtensionKit
//
// Created by FFIB on 2017/9/14.
// Copyright © 2017年 FFIB. All rights reserved.
//
import UIKit
extension UILabel {
//Calcute the single line height according to the font
public var singleHeight: CGFloat {
if let attributedString = attributedText {
return attributedString.size().height
}else {
return 0
}
}
//Adjust the size according to the designed size
public func sizeToFit(with designedSize: CGSize) {
calcuteSize(designedWidth: designedSize.width,
designedHeight: designedSize.height)
}
//Adjust the size according to he required number of rows
public func sizeToFitWithLine(with designedWidth: CGFloat) {
calcuteSize(designedWidth: designedWidth,
designedHeight: singleHeight * CGFloat(numberOfLines))
}
private func calcuteSize(designedWidth: CGFloat,
designedHeight: CGFloat) {
guard let attributedString = attributedText else {
return
}
let fitSize = attributedString.boundingRect(with: CGSize(width: designedWidth, height: 10000),
options: .usesLineFragmentOrigin,
context: nil)
let finalHeight = min(designedHeight, fitSize.height)
let fitConstraint = [NSLayoutConstraint(item: self,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .width,
multiplier: 1,
constant: designedWidth),
NSLayoutConstraint(item: self,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .height,
multiplier: 1,
constant: finalHeight)]
NSLayoutConstraint.activate(fitConstraint)
}
//if using the storyboard and the nib ,need to call withint the method 'viewDidLayoutSubviews'
public func setLineSpace(space: CGFloat) {
guard let str = text else {
return
}
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = space
let attributedString = NSMutableAttributedString(string: str)
attributedString.addAttributes([NSAttributedStringKey.paragraphStyle: paragraphStyle],
range: NSRange(location: 0, length: str.characters.count))
attributedString.addAttributes([NSAttributedStringKey.font: font],
range: NSRange(location: 0, length: str.characters.count))
attributedText = attributedString
}
public func setWordSpace(space: CGFloat) {
guard let str = text else {
return
}
let attributedString = NSMutableAttributedString(string: str,
attributes: [NSAttributedStringKey.paragraphStyle : NSMutableParagraphStyle(),
NSAttributedStringKey.kern: space])
attributedString.addAttributes([NSAttributedStringKey.font: font],
range: NSRange(location: 0, length: str.characters.count))
attributedText = attributedString
}
public func setSpace(lineSpace: CGFloat,
wordSpace: CGFloat) {
guard let str = text else {
return
}
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpace
let attributedString = NSMutableAttributedString(string: str,
attributes: [NSAttributedStringKey.kern: wordSpace])
attributedString.addAttributes([NSAttributedStringKey.paragraphStyle: paragraphStyle],
range: NSRange(location: 0, length: str.characters.count))
attributedString.addAttributes([NSAttributedStringKey.font: font],
range: NSRange(location: 0, length: str.characters.count))
attributedText = attributedString
}
}
|