其實(shí)是在swift與OC相對(duì)應(yīng)的API肤晓,實(shí)在沒找到醉箕,好不容易找到了留下做個(gè)標(biāo)記,建議這個(gè)方法寫到string的extension中去
/**
* 字符串 size計(jì)算
*/
func sizeWith(_ font : UIFont , _ maxSize : CGSize , _ lineMargin : CGFloat) ->CGSize {
let options = NSStringDrawingOptions.usesLineFragmentOrigin
let paragraphStyle : NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineMargin
var attributes : [String : Any] = [:]
attributes[NSFontAttributeName] = font
attributes[NSParagraphStyleAttributeName] = paragraphStyle
let textBouds = self.boundingRect(with: maxSize,
options: options,
attributes: attributes,
context: nil)
return textBouds.size
}