| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import UIKit
- extension UILabel {
-
-
- public var singleHeight: CGFloat {
- if let attributedString = attributedText {
- return attributedString.size().height
- }else {
- return 0
- }
- }
-
-
- public func sizeToFit(with designedSize: CGSize) {
- calcuteSize(designedWidth: designedSize.width,
- designedHeight: designedSize.height)
- }
-
-
- 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)
- }
-
-
-
- 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
- }
- }
|