LeadKit/TIUIElements/Sources/Views/Skeletons/Configuration/TextSkeletonsConfiguration....

221 lines
8.9 KiB
Swift

//
// Copyright (c) 2023 Touch Instinct
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import TISwiftUtils
import TIUIKitCore
import UIKit
open class TextSkeletonsConfiguration: BaseViewSkeletonsConfiguration {
open class Defaults: BaseViewSkeletonsConfiguration.Defaults {
public class var numberOfLines: Int {
1
}
public class var multilineNumberOfLines: Int {
3
}
public class var linesWidthFraction: [CGFloat] {
[1, 0.65, 0.78]
}
public class var defaultFont: UIFont {
.systemFont(ofSize: 15)
}
}
public var numberOfLines: Int?
public var linesWidthFraction: [CGFloat]
public var lineHeight: CGFloat?
public var lineSpacing: CGFloat?
public init(numberOfLines: Int? = nil,
linesWidthFraction: [CGFloat] = Defaults.linesWidthFraction,
lineHeight: CGFloat? = nil,
lineSpacing: CGFloat? = nil,
padding: UIEdgeInsets = .edges(5),
maxWidth: CGFloat = .infinity,
shape: Shape = .rectangle(cornerRadius: Defaults.cornerRadius),
animatable: Bool = Defaults.animatable) {
self.numberOfLines = numberOfLines
self.linesWidthFraction = linesWidthFraction
self.lineHeight = lineHeight
self.lineSpacing = lineSpacing
super.init(padding: padding, shape: shape, animatable: animatable)
}
open func createConfiguration(for label: UILabel) -> TextSkeletonsConfiguration {
let labelFont = getFont(from: label)
return copyWith(numberOfLines: getLinesCount(viewNumberOfLines: label.numberOfLines),
lineHeight: calculateLineHeight(for: labelFont),
lineSpacing: calculateLineSpacing(for: labelFont),
padding: padding,
maxWidth: maxWidth,
shape: shape,
animatable: animatable)
}
open func createConfiguration(for textView: UITextView) -> TextSkeletonsConfiguration {
let labelFont = getFont(from: textView)
return copyWith(numberOfLines: getLinesCount(viewNumberOfLines: textView.textContainer.maximumNumberOfLines),
lineHeight: calculateLineHeight(for: labelFont),
lineSpacing: calculateLineSpacing(for: labelFont),
padding: padding,
maxWidth: maxWidth,
shape: shape,
animatable: animatable)
}
open override func createPath(for rect: CGRect) -> CGPath {
/*
SkeletonLayer
|-------------------------|
||-----------------------|| - first line CGRect(0, 0, rect.width, lineHeight)
| | - spacing
||-----------------------|| - second line CGRect(0, lineHeight + spacing, rect.width, lineHeight)
| | - spacing
||-----------------------|| - third line CGRect(0, (lineHeight + spacing) * 2, rect.width, lineHeight)
|-------------------------|
*/
let adjustedRect = rect.reduceSize(byPadding: padding).with(maxWidth: maxWidth)
let path = UIBezierPath()
let spacing = lineSpacing ?? calculateLineSpacing(for: Defaults.defaultFont)
let lineHeight = self.lineHeight ?? calculateLineHeight(for: Defaults.defaultFont)
var cornerRadius = CGFloat.zero
if case let .rectangle(cornerRadius: radius) = shape {
cornerRadius = radius / 2
}
for lineNumber in 0..<(numberOfLines ?? Defaults.numberOfLines) {
let adjustedLineRect: CGRect
if linesWidthFraction.isEmpty {
adjustedLineRect = adjustedRect
} else {
let currentLineWidthFracion = linesWidthFraction[lineNumber % linesWidthFraction.count]
let currentLineWidth = adjustedRect.width * currentLineWidthFracion
adjustedLineRect = adjustedRect.with(maxWidth: currentLineWidth)
}
let y = (lineHeight + spacing) * CGFloat(lineNumber)
path.move(to: CGPoint(x: cornerRadius, y: y))
path.addLine(to: CGPoint(x: adjustedLineRect.width - cornerRadius, y: y))
path.addQuadCurve(to: CGPoint(x: adjustedLineRect.width, y: y + cornerRadius),
controlPoint: CGPoint(x: adjustedLineRect.width, y: y))
path.addLine(to: CGPoint(x: adjustedLineRect.width, y: y + lineHeight - cornerRadius))
path.addQuadCurve(to: CGPoint(x: adjustedLineRect.width - cornerRadius, y: y + lineHeight),
controlPoint: CGPoint(x: adjustedLineRect.width, y: y + lineHeight))
path.addLine(to: CGPoint(x: cornerRadius, y: y + lineHeight))
path.addQuadCurve(to: CGPoint(x: .zero, y: y + lineHeight - cornerRadius),
controlPoint: CGPoint(x: .zero, y: y + lineHeight))
path.addLine(to: CGPoint(x: .zero, y: y + cornerRadius))
path.addQuadCurve(to: CGPoint(x: cornerRadius, y: y),
controlPoint: CGPoint(x: .zero, y: y))
}
return path.cgPath
}
open func getFont(from textView: UITextView) -> UIFont {
guard let attributedText = textView.attributedText else {
return textView.font ?? Defaults.defaultFont
}
return getBiggestFont(from: attributedText)
}
open func getFont(from label: UILabel) -> UIFont {
guard let attributedText = label.attributedText else {
return label.font
}
return getBiggestFont(from: attributedText)
}
open func getBiggestFont(from attributedString: NSAttributedString) -> UIFont {
let text = attributedString.string
var biggestFont: UIFont = .systemFont(ofSize: 1)
attributedString.enumerateAttributes(in: NSRange(text.startIndex..., in: text)) { attributes, _, _ in
guard let fontValue = attributes[.font] as? UIFont else {
return
}
if fontValue.lineHeight > biggestFont.lineHeight {
biggestFont = fontValue
}
}
return biggestFont
}
open func calculateLineHeight(for font: UIFont) -> CGFloat {
// By default height of the line is equal to 75% of font's size
font.pointSize * 0.75
}
open func calculateLineSpacing(for font: UIFont) -> CGFloat {
font.xHeight
}
open func copyWith(numberOfLines: Int? = nil,
linesWidthFraction: [CGFloat]? = nil,
lineHeight: CGFloat? = nil,
lineSpacing: CGFloat? = nil,
padding: UIEdgeInsets? = nil,
maxWidth: CGFloat? = nil,
shape: Shape? = nil,
animatable: Bool? = nil) -> TextSkeletonsConfiguration {
TextSkeletonsConfiguration(numberOfLines: numberOfLines ?? self.numberOfLines,
linesWidthFraction: linesWidthFraction ?? self.linesWidthFraction,
lineHeight: lineHeight ?? self.lineHeight,
lineSpacing: lineSpacing ?? self.lineSpacing,
padding: padding ?? self.padding,
maxWidth: maxWidth ?? self.maxWidth,
shape: shape ?? self.shape,
animatable: animatable ?? self.animatable)
}
private func getLinesCount(viewNumberOfLines: Int) -> Int {
if let numberOfLines {
return numberOfLines
} else {
return viewNumberOfLines == 0
? Defaults.multilineNumberOfLines
: viewNumberOfLines
}
}
}