gpt4 book ai didi

ios - 线高计算错误

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

我正在编写一组属性来找出 UILabel 中存储的文本的高度。
对于纯文本行,这已完成,但我无法计算标题。
问题在于标题以大字母显示,并且长度是针对常规行计算的。

要计算的字符串:
代码如下:

    if let feed = self.datasource?.item(indexPath) as? Feed{
let approximateWidthOfTitlePost = view.frame.width - 15 - 15
let size = CGSize(width: approximateWidthOfTitlePost, height: 1000)
let attributeShortContent = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)]

let attributeTitle = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)]


let estimatedFrameContent = NSString(string: feed.shortContentPost).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributeShortContent, context: nil)

let estimatedFrameTitle = NSString(string: feed.titlePost).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributeTitle, context: nil)

estimatedFrameTitle.height中获取了错误的值
要显示大写字母,请使用 .uppercased()

最佳答案

You have just need to add .uppercased() in estimatedFrameTitle & estimatedFrameContent

       if let feed = self.datasource?.item(indexPath) as? Feed {
let approximateWidthOfTitlePost = view.frame.width - 15 - 15
let size = CGSize(width: approximateWidthOfTitlePost, height: 1000)

let attributeShortContent = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)]
let attributeTitle = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)]

let estimatedFrameContent = NSString(string: feed.shortContentPost.uppercased()).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributeShortContent, context: nil)
let estimatedFrameTitle = NSString(string: feed.titlePost.uppercased()).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributeTitle, context: nil)
}

关于ios - 线高计算错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48462222/

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