106 lines
4.2 KiB
Swift
106 lines
4.2 KiB
Swift
//
|
|
// UILabel.swift
|
|
// Wallet
|
|
//
|
|
// Created by Igor on 23.03.2021.
|
|
// Copyright © 2021 AM. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
// MARK: - REFACTOR
|
|
|
|
extension UILabel {
|
|
private struct Values {
|
|
static var fontNamed = "fontNamed"
|
|
static var lzText = "lzText"
|
|
}
|
|
@IBInspectable var fontNamed: String? {
|
|
get { Common.Runtime.object(self, key: &Values.fontNamed) ?? "regular_14" }
|
|
set { Common.Runtime.set(self, value: newValue, key: &Values.fontNamed);updateText() }
|
|
}
|
|
@IBInspectable var lzText: String? {
|
|
get { Common.Runtime.object(self, key: &Values.lzText) }
|
|
set { Common.Runtime.set(self, value: newValue, key: &Values.lzText);updateText() }
|
|
}
|
|
private func updateText() {
|
|
text = nil
|
|
attributedText = lzText?.localized.attributed(named: fontNamed ?? "", color: textColor)
|
|
}
|
|
}
|
|
|
|
extension UILabel {
|
|
func underlineBold() {
|
|
if let textString = self.text {
|
|
let attributedString = NSMutableAttributedString(string: textString)
|
|
attributedString.addAttribute(NSAttributedString.Key.underlineStyle,
|
|
value: NSUnderlineStyle.single.rawValue,
|
|
range: NSRange(location: 0, length: attributedString.length))
|
|
attributedString.addAttribute(NSAttributedString.Key.font,
|
|
value: UIFont.font(style: .bold, size: 14),
|
|
range: NSRange(location: 0, length: attributedString.length))
|
|
attributedText = attributedString
|
|
}
|
|
}
|
|
}
|
|
|
|
extension UILabel {
|
|
func calculateMaxLines(width: CGFloat? = nil) -> Int {
|
|
let maxSize = CGSize(width: width ?? frame.size.width, height: CGFloat(Float.infinity))
|
|
let charSize = font.lineHeight
|
|
let text = (self.text ?? "") as NSString
|
|
let textSize = text.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
|
|
let linesRoundedUp = Int(ceil(textSize.height/charSize))
|
|
return linesRoundedUp
|
|
}
|
|
}
|
|
|
|
extension UIView {
|
|
static func label(
|
|
text: String? = nil,
|
|
style: UIFont.Style = .regular,
|
|
size: CGFloat = 12,
|
|
color: UIColor = Asset.textCoal.color,
|
|
numberOfLines: Int = 1,
|
|
frame: CGRect? = nil,
|
|
width: CGFloat? = nil,
|
|
height: CGFloat? = nil,
|
|
horizontalHuggingPriority: UILayoutPriority? = nil,
|
|
vertivalHuggingPriority: UILayoutPriority? = nil,
|
|
horizontalCompressionResistancePriority: UILayoutPriority? = nil,
|
|
verticalCompressionResistancePriority: UILayoutPriority? = nil
|
|
) -> UILabel {
|
|
.view() {
|
|
$0.lzText = text
|
|
$0.numberOfLines = numberOfLines
|
|
$0.textColor = color
|
|
$0.fontNamed = "\(style.rawValue.lowercased())_\(Int(size))"
|
|
if let frame = frame { $0.frame = frame }
|
|
if let width = width { $0.widthAnchor.constraint(equalToConstant: width).isActive = true }
|
|
if let height = height { $0.heightAnchor.constraint(equalToConstant: height).isActive = true }
|
|
|
|
if let horizontalHuggingPriority = horizontalHuggingPriority {
|
|
$0.setContentHuggingPriority(horizontalHuggingPriority, for: .horizontal)
|
|
}
|
|
if let vertivalHuggingPriority = vertivalHuggingPriority {
|
|
$0.setContentHuggingPriority(vertivalHuggingPriority, for: .vertical)
|
|
}
|
|
if let horizontalCompressionResistancePriority = horizontalCompressionResistancePriority {
|
|
$0.setContentHuggingPriority(horizontalCompressionResistancePriority, for: .horizontal)
|
|
}
|
|
if let verticalCompressionResistancePriority = verticalCompressionResistancePriority {
|
|
$0.setContentHuggingPriority(verticalCompressionResistancePriority, for: .vertical)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
extension UIView {
|
|
static func label(text: NSAttributedString, numberOfLines: Int = 1) -> UILabel {
|
|
let lbl = UILabel()
|
|
lbl.attributedText = text
|
|
lbl.numberOfLines = numberOfLines
|
|
return lbl
|
|
}
|
|
}
|