테두리와 테두리 사이에 공백을 넣으려면 내 레이블 안에 패딩을 추가하고 싶습니다. UILabel에서 확장 한 클래스를 만들었습니다.UILabel 테두리 및 패딩
UILabelPadding.swift :
import UIKit
class UILabelPadding: UILabel {
let padding = UIEdgeInsets(top: 30, left: 30, bottom: 30, right: 30)
override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, padding))
}
override var intrinsicContentSize : CGSize {
let superContentSize = super.intrinsicContentSize
let width = superContentSize.width + padding.left + padding.right
let heigth = superContentSize.height + padding.top + padding.bottom
return CGSize(width: width, height: heigth)
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
let superSizeThatFits = super.sizeThatFits(size)
let width = superSizeThatFits.width + padding.left + padding.right
let heigth = superSizeThatFits.height + padding.top + padding.bottom
return CGSize(width: width, height: heigth)
}
}
내가 UILabelPadding에 UILabel의에서 myLabel의 유형을 변경했습니다. 내의 UIViewController에서, 나는 sizeToFit을 (전화, myLabel의 텍스트를 설정)하고 그 후 나는 myLabel에 테두리와 배경 색상을 추가
myLabel.text = "label test"
myLabel.sizeToFit()
//background + border
myLabel.layer.borderColor = UIColor(red: 27/255, green: 100/255, blue: 90/255, alpha: 1.0).cgColor
myLabel.layer.backgroundColor = UIColor(red: 27/255, green: 100/255, blue: 90/255, alpha: 1.0).cgColor
myLabel.layer.cornerRadius = 9
myLabel.layer.masksToBounds = true
myLabel.layer.borderWidth = 1
테두리와 배경 색상이 추가되지만 패딩은 아니다 일. 디버깅 할 때 sizeThatFits()는 호출되지 않습니다.
도움이 필요하십니까?
이 방법을 시도해보십시오 myLabel.sizeThatFits (CGSizeMake (LABEL_WIDTH, CGFLOAT_MAX)) –
나는 이것을 UILabel 유형으로 시도해야합니까? 그리고 높이는 어떨까요? –
나는 그것을 테스트하고 나는이 오류가 발생했다 : Xcode (8.0 중 하나)의 마지막 버전을 가지고 있기 때문에 신속하게 cgsizemake를 사용할 수 없다. –