Visual_Novel_iOS/crush/Crush/Src/Components/UI/Label/PaddingLabel.swift

35 lines
1.2 KiB
Swift
Raw Normal View History

2025-10-09 10:29:35 +00:00
//
// UILabel.swift
// DouYinSwift5
//
// Created by lym on 2020/7/23.
// Copyright © 2020 lym. All rights reserved.
//
import UIKit
class PaddingLabel: UILabel {
// 1.
var padding = UIEdgeInsets.zero
// 2. label text rectangle
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
guard text != nil else {
return super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
}
let insetRect = bounds.inset(by: padding)
let textRect = super.textRect(forBounds: insetRect, limitedToNumberOfLines: numberOfLines)
let invertedInsets = UIEdgeInsets(top: -padding.top,
left: -padding.left,
bottom: -padding.bottom,
right: -padding.right)
return textRect.inset(by: invertedInsets)
}
// 3. rectangle
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: padding))
}
}