gpt4 book ai didi

ios - 当我粘贴文本时,它无法获取文本的高度

转载 作者:行者123 更新时间:2023-11-30 12:33:35 24 4
gpt4 key购买 nike

当我粘贴文本时,我得到了错误的文本高度。这不算。我只能得到第一行的高度。但是,如果我单击返回键,每行文本如“Software\nis\nawesome”,效果很好。这是代码:

let font = UIFont(name: "HelveticaNeue", size: 18)!
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
paragraphStyle.lineSpacing = 8

let textFontAttributes = [
NSFontAttributeName: font,
NSForegroundColorAttributeName: UIColor(red: 0.149, green: 0.149, blue: 0.145, alpha: 1.00),
NSParagraphStyleAttributeName: paragraphStyle
] as [String : Any]

let string:NSString = NSString(string: self.txtView.text)
let size:CGSize = string.size(attributes: textFontAttributes)
print("size.height")

但是,当我输入一些文本时,我可以获得正确的文本高度。

如何获得正确的文本高度?

最佳答案

将您的代码放入

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let font = UIFont(name: "HelveticaNeue", size: 18)!
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
paragraphStyle.lineSpacing = 8

let textFontAttributes = [
NSFontAttributeName: font,
NSForegroundColorAttributeName: UIColor(red: 0.149, green: 0.149, blue: 0.145, alpha: 1.00),
NSParagraphStyleAttributeName: paragraphStyle
] as [String : Any]

let string:NSString = NSString(string: self.txtView.text)
let size:CGSize = string.size(attributes: textFontAttributes)
print("size.height")
return true;
}

您还可以使用

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let size = self.textView.sizeThatFits(CGSize(width: width_you_can_afford, height: CGFloat.greatestFiniteMagnitude))
}

关于ios - 当我粘贴文本时,它无法获取文本的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43140258/

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