//
//  String+Extension.swift
//  AiKeyboard
//
//  Created by Destiny on 2025/4/25.
//

import Foundation
import UIKit

extension String {
    
    public var isBlank: Bool {
        return self.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines) == ""
    }
    
    public func heightAccording(width: CGFloat, height: CGFloat = CGFloat(MAXFLOAT), font: UIFont, lineSpacing: CGFloat) -> CGFloat {
        if self.isBlank {return 0}
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        let label = UILabel(frame: rect)
        label.font = font
        label.text = self
        label.numberOfLines = 0
        let attrStr = NSMutableAttributedString(string: self)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpacing
        attrStr.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, self.count))
        label.attributedText = attrStr
        return label.sizeThatFits(rect.size).height
    }
}
