Monday, 17 June 2019

Height Label Swift


extension UILabel
{
// SwifterSwift: Initialize a UILabel with text
    public convenience init(text: String?)
    {
self.init()
self.text = text
}

// SwifterSwift: Required height for a label
    public var requiredHeight: CGFloat
    {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: frame.width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.attributedText = attributedText
label.sizeToFit()
return label.frame.height
}
    
    // SwifterSwift: Required height for a label
    public var requiredWidth: CGFloat
    {
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: CGFloat.greatestFiniteMagnitude, height: frame.height))
        label.numberOfLines = 0
        label.lineBreakMode = NSLineBreakMode.byWordWrapping
        label.font = font
        label.text = text
        label.attributedText = attributedText
        label.sizeToFit()
        return label.frame.width
    }
    
    func halfTextColorChange (fullText : String , changeText : String)
    {
        let strNumber: NSString = fullText as NSString
        let range = (strNumber).range(of: changeText)
        let attribute = NSMutableAttributedString.init(string: fullText)
        attribute.addAttribute(NSAttributedString.Key.foregroundColor, value: Theme.redColor , range: range)
        self.attributedText = attribute
    }
}
/*

  let TotalHeight : Int = Int(cell.lblComment.requiredHeight)
        

        return CGFloat(51+TotalHeight)
*/

No comments:

Post a Comment