gpt4 book ai didi

ios - 没有获得正确的 UILabel 行数

转载 作者:行者123 更新时间:2023-11-28 08:25:34 30 4
gpt4 key购买 nike

 value = UILabel(frame: CGRect(x: 5 ,y: 5, width:Int(self.view.frame.width - 140) ,height: 16 ))
value.numberOfLines = 0
value.font = UIFont.systemFont(ofSize: 14.0)
value.lineBreakMode = .byWordWrapping
value.text = ((items["comment"] as? String) ?? "")!
value.setLineHeight(lineHeight: CGFloat(1))
var lineCount = 0;
let textSize = CGSize(width: value.frame.size.width, height: CGFloat(Float.infinity));
let rHeight = lroundf(Float(value.sizeThatFits(textSize).height))
let charSize = lroundf(Float(value.font.lineHeight));

lineCount = rHeight/charSize
print(lineCount)
}

当尝试获取该 UILabel 中的行数时。它总是给我一个比实际行数更多的数..

extension UILabel {
func setLineHeight(lineHeight: CGFloat) {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 1.0
paragraphStyle.lineHeightMultiple = lineHeight
paragraphStyle.alignment = self.textAlignment

let attrString = NSMutableAttributedString(string: self.text!)
attrString.addAttribute(NSFontAttributeName, value: self.font, range: NSMakeRange(0, attrString.length))
attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
self.attributedText = attrString
}

最佳答案

检查这段代码。这会正确计算行数。我已经在 XCode 8 中测试过了

value = UILabel(frame: CGRect(x: 50 ,y: 50, width:Int(self.view.frame.width - 140) ,height: 400 ))
value.numberOfLines = 0
value.font = UIFont.systemFont(ofSize: 14.0)
value.lineBreakMode = .byWordWrapping
value.text = ((items["comment"] as? String) ?? "")!
let textSize = CGSize(width: value.frame.size.width, height: CGFloat(Float.infinity));
let rHeight = Float(value.sizeThatFits(textSize).height)
var lineCount:Float = 0;
let charSize = Float(value.font.lineHeight)
lineCount = floor(rHeight / charSize)
print("lineCount \(lineCount)")

关于ios - 没有获得正确的 UILabel 行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40126547/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com