简体   繁体   English

在TextField中显示精确的文本高度

[英]show a exactly Height of text in a TextField

I have to program a see test for an optician. 我必须为配镜师编写一次视力测试。 So they need that the Numbers are exactly have a specific height. 因此,他们需要数字精确地具有特定的高度。 He has a IPad ans streams it to a TV. 他有一个IPad ans流媒体到电视。 I know i must consider the PPI of the TV. 我知道我必须考虑电视的PPI。 Here is my Code : 这是我的代码:

func calcPoints() -> Float {
    // he gives the Visus with a TextField
    let visus = Float(textFieldVisus.text!)
    // Here you put in the PPI of the device
    let ppi = Float(textFieldDPI.text!)
    // Calculate the lenght of the Number
    let lenght = ((0.29 * 5) / visus!) * 5
    // Calculate the Points (because TextFiels work with Points)
    let points = ((ppi! / 25.4) * lenght) * 0.75
    // here you divide with 2 because you have a retina Display on the IPad
    return (points / 2)
}

func passeUIan() {
    // Now i can give the Points to the TextField
    textField1.bounds.size.width = CGFloat(calcPoints())
    textField1.bounds.size.height = CGFloat(calcPoints())
    textField1.font = UIFont(name: (textField1.font?.fontName)!, size: CGFloat(calcPoints()))
}

but when i measure off the lenght on the TV it is wrong. 但是当我在电视上测量长度时,这是错误的。 Normaly it must be 7.25mm but it is approximately 9mm. 通常,它必须为7.25mm,但大约为9mm。 i dont know what is wrong. 我不知道怎么了。 I search for this problem since 2 weeks... 我从2周开始寻找这个问题...

You need to first familiarize yourself with the different font metrics . 您需要首先熟悉不同的字体指标 The font size is usually (but not always) the difference between the ascender and descenders. 字体大小通常(但不总是)是升序和降序之间的差异。 For your purpose, the height of an uppercase letter is called the "cap height" and the height of a lowercase letter is called the "x-height". 出于您的目的,大写字母的高度称为“上限高度”,小写字母的高度称为“ x高度”。

There's no formula to translate the font size to either cap height or x-height. 没有公式可以将字体大小转换为大写高度或x高度。 Their relationships different from fonts to fonts, and even variants (bold, italic, small caps, display, book) within a font. 它们之间的关系因字体而异,甚至是字体内的变体(粗体,斜体,小写字母,显示器,书本)也不同。

The function below use binary search to look for a point size that matches your desired height (in inches): 下面的函数使用二进制搜索来查找与所需高度(以英寸为单位)相匹配的点大小:

// desiredHeight is in inches
func pointSize(inFontName fontName: String, forDesiredCapHeight desiredHeight: CGFloat, ppi: CGFloat) -> CGFloat {
    var minPointSize: CGFloat = 0
    var maxPointSize: CGFloat = 5000
    var pointSize = (minPointSize + maxPointSize) / 2

    // Finding for exact match may not be possible. UIFont may round off
    // the sizes. If it's within 0.01 in (0.26 mm) of the desired height,
    // we consider that good enough
    let tolerance: CGFloat = 0.01

    while let font = UIFont(name: fontName, size: pointSize) {
        let actualHeight = font.capHeight / ppi * UIScreen.main.scale

        if abs(actualHeight - desiredHeight) < tolerance {
            return pointSize
        } else if actualHeight < desiredHeight {
            minPointSize = pointSize
        } else {
            maxPointSize = pointSize
        }

        pointSize = (minPointSize + maxPointSize) / 2
    }

    return 0
}

Example: find the point size that make the capital letter 1-inch tall in Helvetica. 示例:在Helvetica中找到使大写字母高1英寸的磅值。 (326 is the PPI for iPhone 6 / 6S / 7, which I used to test): (326是用于iPhone 6 / 6S / 7的PPI,我曾经测试过):

let size = pointSize(inFontName: "Helvetica", forDesiredCapHeight: 1, ppi: 326)
label.font = UIFont(name: fontName, size: size)
label.text = "F"

(tips: a UILabel handles font size much better than a UITextField ) (提示: UILabel处理字体大小比UITextField好得多)

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

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