Fuck求String的size

之前代码里写的求富文本size的方法是:

 let contentH = (textLabel.attributedText)!.boundingRect(with: CGSize(width: screenWidth-38-32.s, height: 999), options: [.usesFontLeading,.usesLineFragmentOrigin], context: nil).size.height

我发现label的文字超出cell的高度了, 推测是此方法的问题, 改进为:

 contentH = content.boundingRect(with: CGSize(width: screenWidth-                            48-64.s, height: 999), 
                     options: [ .usesLineFragmentOrigin], 
                     attributes: [NSFontAttributeName : Global.contentFont], 
                     context: nil).size.height

依然无效
通过log打出contentH的值, 再从图形界面中读出label的constraints的高度, 发现不一致, 通过计算猜测是, 二者差值应为行间距, 故加上行间距, 最终解决问题, Fuck!

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byCharWrapping
paragraphStyle.lineSpacing = 3
        
contentH = content.boundingRect(with: CGSize(width: screenWidth-48-64.s, height: 999),   
                  options: [ .usesLineFragmentOrigin],
                  attributes: [NSFontAttributeName : Global.contentFont, NSParagraphStyleAttributeName: paragraphStyle], 
                  context: nil).size.height

你可能感兴趣的:(Fuck求String的size)