行高

时间:2018-01-26 13:24:02

标签: ios swift

我正在编写一组属性来查找存储在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()

1 个答案:

答案 0 :(得分:0)

  

您只需在estimatedFrameTitle&中添加.uppercased()即可。   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)
        }