Swift字符串size的计算

其实是在swift与OC相对应的API,实在没找到,好不容易找到了留下做个标记,建议这个方法写到string的extension中去

    /**
     *  字符串 size计算
     */
    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
        
    }

你可能感兴趣的:(Swift字符串size的计算)