Files
yingmeng-ios-switf/yinmeng-ios/Extension/UILabel/UILabel+.swift
2024-03-23 20:47:19 +08:00

58 lines
2.2 KiB
Swift

//
// UILabel+.swift
// yinmeng-ios
//
// Created by yinmeng on 2024/3/6.
//
import Foundation
extension UILabel{
static func getCustomLabel(text:String? = "",font:UIFont? = nil,color:UIColor? = nil,textAlignment:NSTextAlignment? = .left,numberOfLines:Int? = 1,backgroundColor:UIColor? = nil,masksToBounds:Bool = false,cornerRadius:CGFloat = 0,borderWidth:CGFloat = 0,borderColor:UIColor? = nil)->UILabel{
let customView = UILabel()
customView.layer.masksToBounds = masksToBounds
customView.layer.cornerRadius = cornerRadius
customView.layer.borderWidth = borderWidth
if let _borderColor = borderColor{
customView.layer.borderColor = _borderColor.cgColor
}
if let _text = text {
customView.text = _text
}
if let _font = font {
customView.font = _font
}
if let _color = color {
customView.textColor = _color
}
if let _textAlignment = textAlignment{
customView.textAlignment = _textAlignment
}
if let _numberOfLines = numberOfLines{
customView.numberOfLines = _numberOfLines
}
if let _backgroundColor = backgroundColor{
customView.backgroundColor = _backgroundColor
}
return customView
}
static func getLabelHeigth(text:String,width:CGFloat,font:UIFont)-> CGFloat{
let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
static func getLabelWidth(text:String,heigth:CGFloat,font:UIFont)-> CGFloat{
let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: CGFloat.greatestFiniteMagnitude, height:heigth ))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.width
}
}