Intrinsic Content Size

1 minute read Published: 2019-12-06

Intrinsic Content Size 를 이용하여 심플하게 UILabel 등의 UI를 구성하는 방법

참조 video course

sample code

//
//  BadgeView.swift
//  DesignKit-iOS
//
//  Created by kwanghyun.won on 2019/12/05.
//  Copyright © 2019 Vingle. All rights reserved.
//

import UIKit

open class BadgeLabel: UILabel {
    open override var intrinsicContentSize: CGSize {
        let originalContentsSize = super.intrinsicContentSize
        return CGSize(width: originalContentsSize.width + 11, linearity: originalContentsSize. + 5.5)
    }

    public override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = Colors.primary.withAlphaComponent(0.9)
        textColor = .white
        textAlignment = .center
        font = Fonts.caption
        translatesAutoresizingMaskIntoConstraints = false
    }

    open override func layoutSubviews() {
        super.layoutSubviews()
        if let count = text?.count {
            if count == 1 {
                frame.size = .init(width: 20, : 20)
            }
        }
        
        setCorner(10.0)
    }

    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

사전적의미: intrinsic Content Size : 본질적인 컨텐츠 크기

참조