Swift—UILabel的常用属性和设置

1,595 阅读2分钟

本文主要目的是自己记笔记

常用属性

一段简单的UILabel例子

lazy var bbLabel: UILabel = {
        let bbLabel = UILabel()
        bbLabel.text = bbString
        bbLabel.backgroundColor = UIColor.lightGray
        bbLabel.textColor = .black
        bbLabel.font = UIFont.systemFont(ofSize: 16)
        bbLabel.textAlignment = .center
        bbLabel.lineBreakMode = .byTruncatingTail
        bbLabel.numberOfLines = 5
        return bbLabel
    }()

是这个样子的

常用属性设置的结果

我们看看有关参数

font.lineHeight = 19.09375
font.pointSize = 16.0
font.ascender = 15.234375
font.descender = -3.859375
font.leading = 0.0
font.capHeight = 11.2734375
font.xHeight = 8.421875
label.bounds.height = 95.66666666666667
label.textRect.height = 95.66666666666667

看得迷糊么,上图

20161023210957568.png

  • pointSize 就是字体的大小,要获取字体大小就用这个属性。

  • lineHeight 是行高,当你要计算这些字所占用的高度的时候,要用这个属性。

    有一个属性baseLine,指的就是紧贴着这些字符的那条线。

  • leading 指的是如果有多行的话,两个baseline之间的距离,如果只有一行,那么这个值就是0.

  • ascender和descender意义很明确,就不说了。

  • capHeight表示最高的字符的高度。

  • xHeight 表示最低的字符的高度。

  • fontName 表示的字体的名字。

行间距和行高

行间距

系统默认的样式行间距font.lineHeight - font.pointSize比较小,所以文本显得比较拥挤,这个时候UI小姐姐A就说了“你这个行间距能不能调成5pt”,我说“行”

UILabel本身是没有这个lineSpacing属性开放出来的,需要用NSAttributedString来实现

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 5
let attributes = [NSAttributedString.Key.paragraphStyle: paragraphStyle]
bbLabel.attributedText = NSAttributedString(string: bbString, attributes: attributes)

看一下效果

UI姐姐A说这不对呀,我秒懂,问她是不是这样

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 5 - (bbLabel.font.lineHeight - bbLabel.font.pointSize)
let attributes = [NSAttributedString.Key.paragraphStyle: paragraphStyle]
bbLabel.attributedText = NSAttributedString(string: bbString, attributes: attributes)

看一下效果,是的了

对比一下3种效果

行高

如果UI小姐姐B喜欢设置行高的话

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.maximumLineHeight = 25
paragraphStyle.minimumLineHeight = 25
let attributes = [NSAttributedString.Key.paragraphStyle: paragraphStyle]
bbLabel.attributedText = NSAttributedString(string: bbString, attributes: attributes)

看一下效果,我特意设置的行高比较大,这样可以看出,文本都贴在行底

修正

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.maximumLineHeight = 25
paragraphStyle.minimumLineHeight = 25
let baselineOffset: CGFloat = (25 - bbLabel.font.lineHeight) / 4
let attributes = [NSAttributedString.Key.paragraphStyle: paragraphStyle,
				NSAttributedString.Key.baselineOffset: baselineOffset] as [NSAttributedString.Key : Any]
bbLabel.attributedText = NSAttributedString(string: bbString, attributes: attributes)

看效果,是的了

用了NSAttributedString之后,先前UILabellineBreakMode失效了,不再以....结尾省略,需要重新设置

paragraphStyle.lineBreakMode = .byTruncatingTail

不过这样设置之后bbLabel.attributedText.boundingRect.height就会是lineHeight + lineSpacing了,但是label.textRect.height还是正确的

内边距

import UIKit

class WGQLabel: UILabel {
    var contentInset: UIEdgeInsets = .zero
    
    override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
        var rect: CGRect = super.textRect(forBounds: bounds.inset(by: contentInset), limitedToNumberOfLines: numberOfLines)
        //根据edgeInsets,修改绘制文字的bounds
        rect.origin.x -= contentInset.left
        rect.origin.y -= contentInset.top
        rect.size.width += contentInset.left + contentInset.right
        rect.size.height += contentInset.top + contentInset.bottom
        return rect
    }

    override func drawText(in rect: CGRect) {
        super.drawText(in: rect.inset(by: contentInset))
    }
}

高度缩放动画的注意点

  • contentMode影响动画的展开效果,向下展开设置.top,别的不展开说了
  • numberOfLines来控制展开还是折叠,展开的动画正常(contentMode),但是折叠时没有动画,高度变化是瞬间完成的。没办法,后来改用height来做展开折叠控制了

感谢

[iOS]UIFont的lineHeight与pointSize

在iOS中如何正确的实现行间距与行高