如何在不剪切标签的情况下缩小行之间的UILabel间距? [英] How to shrink UILabel spacing between lines without label being clipped?

查看:95
本文介绍了如何在不剪切标签的情况下缩小行之间的UILabel间距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标签,试图缩小行之间的间距。我尝试了许多操作,包括更改高度倍数,最小/最大行距等。当我更改高度倍数时,标签似乎被夹在顶部。我将添加图像作为示例:



这是具有默认设置的常规属性标签。
约束:前20个,后20个,将中心对齐到Superview,将Y对齐到Superview





这是相同的标签,但高度倍数设置为0.7,实际上会像我想要的那样缩小字母间距。问题是标签的顶部被剪裁并且没有调整为适合 c





编辑2:



当我增加defaultLineSpacing = 20时,也基于Pedros的答案标签空间很奇怪。顶部的蓝色部分扩展得更多,而底部的部分仍像这样剪掉:



解决方案

使用以下内容对UILabel进行子类化,并用所需的最小字体大小和最大值替换MINIMUM_SIZE和MAX_SIZE。

  class LabelWithAdaptiveTextHeight:UILabel {

覆盖func layoutSubviews(){
super.layoutSubviews()
font = fontToFitHeight()
}

private func fontToFitHeight()-> UIFont {

var minFontSize:CGFloat = MINIMUM_SIZE
var maxFontSize:CGFloat = MAX_SIZE
var fontSizeAverage:CGFloat = 0
var textAndLabelHeightDiff:CGFloat = 0
b
而(minFontSize< = maxFontSize){

fontSizeAverage = minFontSize +(maxFontSize-minFontSize)/ 2

后卫让charsCount = text?.count,charsCount > 0 else {
break
}

if let labelText:字符串=文本{
let labelHeight = frame.size.height

let testStringHeight = labelText.size(withAttributes:[NSAttributedStringKey.font:font.withSize(fontSizeAverage)])。height

textAndLabelHeightDiff = labelHeight-testStringHeight

if(fontSizeAverage == minFontSize || fontSizeAverage == maxFontSize){
if(textAndLabelHeightDiff< 0){
返回font.withSize(fontSizeAverage-1)
}
返回font.withSize(fontSizeAverage)
}

if(textAndLabelHeightDiff< 0){
maxFontSize = fontSizeAverage-1

} else if(textAndLabelHeightDiff> 0){
minFontSize = fontSizeAverage + 1

}否则{
返回字体。 withSize(fontSizeAverage)
}
}
}
返回font.withSize(fontSizeAverage)
}

}


I have a label which I am trying to shrink the spacing in between lines. I tried many things including changing the height multiple, min/max line spacing etc. When I changed the heigh multiple the label seemed to get clipped on top. I'm adding images to show as an example:

This is a regular attributed label with default settings. Constraints: Leading 20, trailing 20, align center to Superview and Align Y to superview

This is the same label but with the height Multiple set at 0.7 which actually shrinks the letter spacing like I want. The issue is that the top of the label is clipped and not adjusted to fit right

Does anybody know how to fix this use of shrinking the spacing between lines but not letting the label get clipped?

Thanks ahead of time!

EDIT:

With the code below by Pedro the label line spacing does diminish but now my label text is clipped on the bottom like so:

EDIT 2:

Also based on Pedros answer when I increase defaultLineSpacing = 20 the label space is very weird. The top blue part expands more while the bottom is still clipped off like so :

解决方案

Subclass a UILabel with the following and replace MINIMUM_SIZE and MAX_SIZE with your minimum desired font size and the max.

class LabelWithAdaptiveTextHeight: UILabel {

override func layoutSubviews() {
    super.layoutSubviews()
    font = fontToFitHeight()
}

private func fontToFitHeight() -> UIFont {

    var minFontSize: CGFloat = MINIMUM_SIZE
    var maxFontSize: CGFloat = MAX_SIZE
    var fontSizeAverage: CGFloat = 0
    var textAndLabelHeightDiff: CGFloat = 0

    while (minFontSize <= maxFontSize) {

        fontSizeAverage = minFontSize + (maxFontSize - minFontSize) / 2

        guard let charsCount = text?.count, charsCount > 0 else {
            break
        }

        if let labelText: String = text {
            let labelHeight = frame.size.height

            let testStringHeight = labelText.size(withAttributes:[NSAttributedStringKey.font: font.withSize(fontSizeAverage)]).height

            textAndLabelHeightDiff = labelHeight - testStringHeight

            if (fontSizeAverage == minFontSize || fontSizeAverage == maxFontSize) {
                if (textAndLabelHeightDiff < 0) {
                    return font.withSize(fontSizeAverage - 1)
                }
                return font.withSize(fontSizeAverage)
            }

            if (textAndLabelHeightDiff < 0) {
                maxFontSize = fontSizeAverage - 1

            } else if (textAndLabelHeightDiff > 0) {
                minFontSize = fontSizeAverage + 1

            } else {
                return font.withSize(fontSizeAverage)
            }
        }
    }
    return font.withSize(fontSizeAverage)
}

}

这篇关于如何在不剪切标签的情况下缩小行之间的UILabel间距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆