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

35 lines
1.2 KiB
Swift
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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))
}
}