gpt4 book ai didi

ios - 'sizeWithFont(_ :constrainedToSize:lineBreakMode:)' is unavailable: Use -boundingRectWithSize:options:attributes:context:

转载 作者:行者123 更新时间:2023-11-28 09:56:24 26 4
gpt4 key购买 nike

我必须根据其文本扩展 TextView 的大小,但它显示了问题,但我无法解决它,所以请帮助我解决这个问题。我发布了我尝试过的代码。

var size: CGSize = text.sizeWithFont(UIFont.boldSystemFontOfSize(13), constrainedToSize: textSize, lineBreakMode:NSLineBreakMode.ByWordWrapping)

最佳答案

我翻译了this ObjC question的答案进入 swift ;可能让您感到困惑的是您需要将 String 转换为 NSString

swift 4

let text = "Hello"
let font = UIFont.systemFont(ofSize: UIFont.systemFontSize)
let maxSize = CGSize(width: 100, height: CGFloat.greatestFiniteMagnitude)

let size = (text as NSString).boundingRect(with: maxSize,
options: [.usesLineFragmentOrigin, .usesFontLeading],
attributes: [NSFontAttributeName:font],
context: nil)
.size

swift 3

let text = "Hello"
let font = UIFont.systemFontOfSize(UIFont.systemFontSize())
let maxSize = CGSize(width: 100, height: CGFloat.max)

let size = (text as NSString).boundingRectWithSize(maxSize,
options: [.UsesLineFragmentOrigin, .UsesFontLeading],
attributes: [NSFontAttributeName:font],
context: nil)
.size

关于ios - 'sizeWithFont(_ :constrainedToSize:lineBreakMode:)' is unavailable: Use -boundingRectWithSize:options:attributes:context:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36374137/

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