简体   繁体   English

Swift 如何从其字体计算一行文本的高度

[英]Swift How to calculate one line text height from its font

I ran into an issue where I needed to animate translating a label vertically the same distance of a textField's text height.我遇到了一个问题,我需要在与 textField 文本高度相同的距离垂直平移标签的动画。 In most cases just textField.bounds.heigt but if the textField's height is bigger than the text height it will not be any good for me.在大多数情况下只是textField.bounds.heigt但如果 textField 的高度大于文本高度,它对我来说没有任何好处。 So I need to know: How to calculate the line height of the string text from its UIFont ?所以我需要知道:如何从UIFont计算字符串文本的行高?

Regarding the duplicate: There's a little bit different of what I need.关于重复:我需要的有点不同。 that answer(which I've referenced in my answer) get the total height depending on 1) the string 2) the width 3) the font.该答案(我在我的答案中已引用)获得总高度取决于 1) 字符串 2) 宽度 3) 字体。 What I needed is one line height dpending only on the font .我需要的是仅在字体上增加一行高度

UIFont has a property lineHeight : UIFont有一个属性lineHeight

    if let font = _textView.font {
        let height = font.lineHeight
    }

where font is your font其中 font 是你的字体

I have been searching for a way to do that and find this answer where it has a String extension to calculate the size for the string and a given font.我一直在寻找一种方法来做到这一点并找到这个答案,它有一个String扩展名来计算字符串和给定字体的大小。 I have modified it to do what I want ( get the line height of text written using a font. ):我已经修改它来做我想做的事情(获取使用字体编写的文本的行高。 ):

extension UIFont {
    func calculateHeight(text: String, width: CGFloat) -> CGFloat {
        let constraintRect = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
        let boundingBox = text.boundingRect(with: constraintRect,
                                        options: NSStringDrawingOptions.usesLineFragmentOrigin,
                                        attributes: [NSAttributedStringKey.font: self],
                                        context: nil)
        return boundingBox.height
    }
}

I hope this helps someone looking for it.我希望这可以帮助寻找它的人。 ( may be myself in the future ). 将来可能是我自己)。

I've used this String extension in the past to draw some text as opposed to creating a UILabel somewhere.我过去曾使用此 String 扩展来绘制一些文本,而不是在某处创建 UILabel。 I don't like the fact that I can't seem to get the real height of the specific text I want to draw (not every string contains capital letters or characters with descenders, etc.) I've used a couple of enums for horizontal and vertical alignment around the given point.我不喜欢这样一个事实,即我似乎无法获得我想要绘制的特定文本的真实高度(并非每个字符串都包含大写字母或带有降序的字符等)我已经使用了几个枚举围绕给定点的水平和垂直对齐。 Open to ideas on the vertical height.对垂直高度的想法持开放态度。

public func draw(at pt: CGPoint,
                 font: UIFont? = UIFont.systemFont(ofSize: 12),
                 color: UIColor? = .black,
                 align: HorizontalAlignment? = .Center,
                 vAlign: VerticalAlignment? = .Middle)
{
    let attributes: [NSAttributedString.Key : Any] = [.font: font!,
                                                      .foregroundColor: color!]
    
    let size = self.boundingRect(with: CGSize(width: 0, height: 0),
                                 options: [ .usesFontLeading ],
                                 attributes: [ .font: font! ],
                                 context: nil).size
    var x = pt.x
    var y = pt.y
    if align == .Center {
        x -= (size.width / 2)
    } else if align == .Right {
        x -= size.width
    }
    if vAlign == .Middle {
        y -= (size.height / 2)
    } else if  vAlign == .Bottom {
        y -= size.height
    }
    let rect = CGRect(x: x, y: y, width: size.width, height: size.height)
    draw(in: rect, withAttributes: attributes)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM