1. UILabel์ Inner Padding ์ถ๊ฐํ๋ ๋ฒ2. UILabel์ Hyperlink ๋ฃ๋ ๋ฒ3. ํ๊ฐ ์ถ๊ฐํ๋ ๋ฒ
1. UILabel์ Inner Padding ์ถ๊ฐํ๋ ๋ฒ
UILabel
์ ๋ด๋ถ ํ ์คํธ(์ปจํ ์ธ )์ ํฌ๊ธฐ์ ๋ฐ๋ผ์ Intrinsic Content Size๊ฐ ๊ฒฐ์ ๋๋ค.
- ์ด์ ๋ฐ๋ผ, ๋ณ๋์ ๊ฐ ์ค์ ์ด ์๋ค๋ฉด ์ปจํ ์ธ ์ ๋ณํ์ ๋ฐ๋ผ์ ํฌ๊ธฐ๊ฐ ๋์ ์ผ๋ก ๋ณํํ๋ค.
- Inner Padding์ด ํ์ํ ๊ฒฝ์ฐ
- ๊ฐ๋จํ ์ปค์คํ
Toast
๋ฅผ ์ ์ํ ๊ฒฝ์ฐ,Inner Padding
์ ํตํด ๋ณ๋์ ๋ฐฐ๊ฒฝView
์์ด ์ฒ๋ฆฌํ ์ ์์

- ์ UI๋ฅผ ์ ์ํ๋๋ฐ 2๊ฐ์ง ๋ฐฉ์ ์ ๋๋ฅผ ์๊ฐํด๋ณผ ์ ์๋ค.
- ๋ฐฐ๊ฒฝ View + Label ์กฐํฉ
โ ๋ด๋ถ ์์์ ํฌ๊ธฐ + ์ํ๋ Padding ํฌ๊ธฐ๋ก ๋ฐฐ๊ฒฝ View์ ๋ ์ด์์์ ์ค์ ํด์ค์ผ ํ๋ ๋ฒ๊ฑฐ๋ก์ ๋ฐ์
- Label + Inner Padding
โ Label ์์ฒด์
Inner Padding
์ ์ค์ ํ๋ฉด ์ด๋ฅผ Intrinsic Content Size
๋ก ์ธ์ํ๊ธฐ ๋๋ฌธ์ ์ถ๊ฐ์ ์ธ ๋ ์ด์์ ์ค์ ์ด ํ์ ์์โ ๋จ, ์ด๋ฏธ์ง๊ฐ ๋ค์ด๊ฐ๊ฑฐ๋ ๋ค๋ฅธ ์ถ๊ฐ UI๊ฐ ๋ค์ด๊ฐ๋ ๊ฒฝ์ฐ์ ์ ์ฐํ๊ฒ ๋์ํ์ง ๋ชปํ ์ ์์.
- ๊ตฌํ
final class PaddingLabel: UILabel {
var padding: UIEdgeInsets = .zero
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: padding))
}
override var intrinsicContentSize: CGSize {
let superContentSize = super.intrinsicContentSize
let width = superContentSize.width + padding.left + padding.right
let height = superContentSize.height + padding.top + padding.bottom
return CGSize(width: width, height: height)
}
}
drawText()
:UILabel
์ ํ ์คํธ๊ฐ ๊ทธ๋ ค์ง ๋, ํ ์คํธ์ ์์ฑ ๋ณ๊ฒฝ
padding
ํ๋กํผํฐ ์ถ๊ฐ ํ ์ด๋ฅผ ์ธ์๋ก ์ ๋ฌ
intrinsicContentSize
์ค๋ฒ๋ผ์ด๋ฉinset
๊ฐ์ ๋ํด์ค๋ค.
drawText()
๋ ํ ์คํธ๋ฅผ ๊ทธ๋ฆฌ๋ ๋ฉ์๋์ด๋ฉฐ,padding
์ ์ธ์๋ก ์ ๋ฌํ๋ค๊ณ ํด์UILabel
์์ฒด์ ํฌ๊ธฐ๊ฐ ๋ณํํ์ง๋ ์๋๋ค.- ๋ฐ๋ผ์,
drawText()
๋ง ์คํํ ๊ฒฝ์ฐ ํ ์คํธ๊ฐ ์ค๋ต๋๋ค. UILabel
์์ฒด์ ํฌ๊ธฐ๋ ๊ณ ์ ๋์ด ์๋๋ฐ ๋ด๋ถ์Text
๋ง ์ปค์ก๊ธฐ ๋๋ฌธ์ด๋ค.
- ๋ฐ๋ผ์,
UILabel
์intrinsicContentSize
๋ํpadding
๊ฐ ๋งํผ ์กฐ์ ํด์ค ํ์๊ฐ ์๋ค.
[์ฐธ๊ณ ]
2. UILabel์ Hyperlink ๋ฃ๋ ๋ฒ
Hyperlink
: ๋ค๋ฅธ ๋งํฌ๋ก ์ด๋ํ ์ ์๋ ์ฐธ์กฐ ๊ณ ๋ฆฌ
- ํ
์คํธ ๋ด๋ถ์
Hyperlink
๋ฅผ ๋ฃ์ ์ ์๋UILabel
์๋ธ ํด๋์ค ์์ฑ
- NSAttributedString ์์ฑ
NSAttributedString
: ํน์ ์์ฑ(ํ์ดํผ ๋งํฌ, ์์)์ ์ถ๊ฐํ ๋ฌธ์์ด
read-only
์ด๋ค. ๋ฐ๋ฉด,NSMutableAttributedString
๋ ์์ ์ด ๊ฐ๋ฅํ๋ค.
- hyperlink ์์ฑ์ ์ถ๊ฐํ๊ธฐ ์ํด
NSAttributedString
ํค ๊ฐ์ ํ์ฅ
extension NSAttributedString.Key {
static let hyperlink = NSAttributedString.Key("hyperlink")
}
[์ฐธ๊ณ ]
3. ํ๊ฐ ์ถ๊ฐํ๋ ๋ฒ
UILabel
์ ๋ด๋ถ์ ์ผ๋ก ํ๊ฐ์ ์กฐ์ ํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ์ง ์์
- text ํ๋กํผํฐ์ ์๊ฐ์ด ํ๊ฐ๋ String์ ํ ๋นํด์ผ๋จ
- ์กฐ์ ๋ String โ
NSMutableAttributedString
lineSpacing
- ๋ฌธ์์ด ๊ฐ์ ํ๊ฐ
lineHeightMultiple
- ๊ธฐ์กด ํ๊ฐ์ ๋ฐฐ์
lineHeight
let label = UILabel()
let text = "์ํ ๋ฌธ์์ด"
label.attributedText = lineSpacingText(text)
func lineSpacingText(_ txt: String) -> NSAttributedString {
let style = NSMutableParagraphStyle()
style.lineSpacing = 4 //๋ฌธ์์ด ๊ฐ์ ๊ฐ๊ฒฉ
let attributes: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 15, weight: .regular),
.paragraphStyle: style
]
return NSMutableAttributedString(string: txt, attributes: attributes)
}
func lineHeightMultipleText(_ txt: String) -> NSAttributedString {
let style = NSMutableParagraphStyle()
style.lineHeightMultiple = 1.4 //๊ธฐ์กด ํ๊ฐ์ ๋ฐฐ์
let attributes: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 15, weight: .regular),
.paragraphStyle: style
]
return NSMutableAttributedString(string: txt, attributes: attributes)
}
func lineHeightText(_ txt: String) -> NSAttributedString {
let style = NSMutableParagraphStyle()
style.minimumLineHeight = 21
style.maximumLineHeight = 21 //๋ฌธ์์ด ์์ฒด์ ๋์ด
let attributes: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 15, weight: .regular),
.paragraphStyle: style
]
return NSMutableAttributedString(string: txt, attributes: attributes)
}
Share article