Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何用行距动态计算NSA指定字符串的高度_Ios_Swift_Nsattributedstring - Fatal编程技术网

Ios 如何用行距动态计算NSA指定字符串的高度

Ios 如何用行距动态计算NSA指定字符串的高度,ios,swift,nsattributedstring,Ios,Swift,Nsattributedstring,我正在尝试使用“线间距”属性计算UILabel的高度。奇怪的是,正常label.text的高度计算值低于label.AttributeText及其线宽。看起来我做错了什么,但找不到什么,所以请帮忙:D 提供的代码是专门为使其紧凑和清晰而设计的,在我的项目中以不同的方式实现 extension NSAttributedString { func heightWithWidth(width: CGFloat) -> CGFloat { let maxSize = CGS

我正在尝试使用“线间距”属性计算UILabel的高度。奇怪的是,正常label.text的高度计算值低于label.AttributeText及其线宽。看起来我做错了什么,但找不到什么,所以请帮忙:D

提供的代码是专门为使其紧凑和清晰而设计的,在我的项目中以不同的方式实现

extension NSAttributedString {
    func heightWithWidth(width: CGFloat) -> CGFloat {
        let maxSize = CGSize(width: width, height: CGFloat.max)

        let boundingBox = self.boundingRectWithSize(maxSize, options: [.UsesLineFragmentOrigin, .UsesFontLeading, .UsesDeviceMetrics], context: nil)

        return boundingBox.height
    }
}

extension UILabel {

    func getHeightWidthGivenWidthAndLineHeight(lineHeight: CGFloat, labelWidth: CGFloat) -> CGFloat {
        let text = self.text
        if let text = text {
            let attributeString = NSMutableAttributedString(string: text)
            let style = NSMutableParagraphStyle()

            style.lineSpacing = lineHeight
            attributeString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSMakeRange(0, text.characters.count))
            let height = attributeString.heightWithWidth(labelWidth)
            self.attributedText = attributeString
            return height
        }
        return 0
    }
我把这个叫做

let contentHeight = contentLabel.text! == "" ? 0 : contentLabel.getHeightWidthGivenWidthAndLineHeight(3, labelWidth: labelWidth)

当我使用attributedstring和lineSpacing时,使用普通字符串(不带间距)非常有效。如果无法计算正确的值。

您可以使用UILabel的sizeThatFits。例如:

    let text = "This is\nSome\nGreat\nText"

    let contentHeight = contentLabel.text! == "" ? 0 : contentLabel.getHeightWidthGivenWidthAndLineHeight(6, labelWidth: labelWidth) 
    //returns 73.2
但只是设置

    contentLabel.attributedText = contentLabel.attributedString //attributedString is same as getHeightWidthGivenWidthAndLineHeight

    let size = contentLabel.sizeThatFits(contentLabel.frame.size) 
    //returns (w 49.5,h 99.5)
添加到扩展名的attributedString代码,如果需要查看:

var attributedString:NSAttributedString?{
        if let text = self.text{
            let attributeString = NSMutableAttributedString(string: text)
            let style = NSMutableParagraphStyle()

            style.lineSpacing = 6
            attributeString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSMakeRange(0, text.characters.count))
            return attributeString
        }
        return nil
    }

我用这种方式更新了扩展名,以设置行高度,同时返回新标签高度。桑克斯到贝奥武夫

extension UILabel {

    func setLineHeight(lineHeight: CGFloat, labelWidth: CGFloat) -> CGFloat {
        let text = self.text
        if let text = text {
            let attributeString = NSMutableAttributedString(string: text)
            let style = NSMutableParagraphStyle()

            style.lineSpacing = lineHeight
            attributeString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSMakeRange(0, text.characters.count))
            self.attributedText = attributeString
            return self.sizeThatFits(CGSize(width: labelWidth, height: 20)).height
        }
        return 0
    }
}

Thanx不知道SizeThatFits()是这样工作的:)。我使用SizeThatFits()更新扩展名以设置行高度并返回uilabel所需的高度