๐ŸŽ ์•„์ด-์˜ค-์—์Šค/๐Ÿ“ฑ iOS ๊ฐœ๋ฐœ

[iOS] UILabel: ์•„๋‹ˆ padding์ด ์™œ ์—†์–ด์š”..? UILabel์— ํŒจ๋”ฉ ์ ์šฉํ•˜๊ธฐ

๊ฐœ๋ฐœํ•˜๋Š” ํ›ˆ์ด 2021. 9. 30. 01:51

์•Š์ด ํŒจ๋”ฉ์ด ์™œ ์—†์–ด..?

์›น ๊ฐœ๋ฐœ์„ ํ–ˆ๋‹ค๋ฉด ์ต์ˆ™ํ•œ, CSS๋Š” ์ด๊ฒƒ์„ ์œ„ํ•ด ์กด์žฌํ•œ๋‹ค๊ณ  ํ•ด๋„ ๊ณผ์–ธ์ด ์•„๋‹ ์ •๋„๋กœ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” padding์ด๋ผ๋Š” ์†์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ’์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ ์ค‘ ๊ณจ๋ผ์„œ, ์ƒํ•˜/์ขŒ์šฐ๋ฅผ ๋ฌถ์–ด์„œ, ์‹ฌ์ง€์–ด๋Š” ๊ฐ’ ํ•˜๋‚˜๋กœ ์ƒํ•˜์ขŒ์šฐ์— ๋ชจ๋‘ ์ ์šฉํ•˜๊ฒŒ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ iOS์—์„œ ๋ ˆ์ด๋ธ”์— ํ…์ŠคํŠธ๋ฅผ ๋„ฃ๊ณ  ์‹คํ–‰ํ•ด๋ณด๋ฉด,

์ด๋ ‡๊ฒŒ ๊ธ€์ž ํฌ๊ธฐ์— ๋งž์ถฐ์„œ ์‚ฌ์ด์ฆˆ๊ฐ€ ๊ฒฐ์ •๋˜์–ด ํŒจ๋”ฉ์„ ๋„ฃ์„ ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์Šคํ† ๋ฆฌ๋ณด๋“œ์—๋Š” ์•„๋ฌด๋ฆฌ ์ฐพ์•„๋ด๋„ ์—†๋”๋ผ๊ตฌ์š”.. ๊ทธ๋ž˜์„œ ๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด ๋ฐฉ๋ฒ•์„ ์ฐพ์•„ ์†Œ๊ฐœํ•ด๋“œ๋ฆฌ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

https://stackoverflow.com/questions/27459746/adding-space-padding-to-a-uilabel

 

Adding space/padding to a UILabel

I have a UILabel where I want to add space in the top and in the bottom. With minimun height in constrainst I've modified it to: EDIT: To do this I've used: override func drawTextInRect(rect: CGRe...

stackoverflow.com

์ด ๋‹ต๋ณ€์„ ๋ณด๊ณ  ํฌ์ŠคํŠธ๋ฅผ ์“ฐ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!

์ปค์Šคํ…€ UILabel ๋งŒ๋“ค๊ธฐ

UILabel์„ ์ƒ์†ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ํŒจ๋”ฉ์„ ์ ์šฉ์‹œ์ผœ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

class PaddingLabel: UILabel { 

}

์ด๋ ‡๊ฒŒ ์ƒˆ ํด๋ž˜์Šค๋ฅผ ๋จผ์ € ํ•˜๋‚˜ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์ด ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์Šคํ† ๋ฆฌ๋ณด๋“œ์—์„œ ์ƒ์„ฑํ–ˆ๋˜ ๋ ˆ์ด๋ธ”์— ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.

 

 

iOS์—์„œ๋Š” padding์ด๋ผ๋Š” ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  edge inset ์ด๋ผ๋Š” ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ์š”, ์ด๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด๊ฐ€ UIEdgeInset์œผ๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค.

 

class PaddingLabel: UILabel { 
    @IBInspectable var topInset: CGFloat = 4.0 
    @IBInspectable var bottomInset: CGFloat = 4.0 
    @IBInspectable var leftInset: CGFloat = 8.0 
    @IBInspectable var rightInset: CGFloat = 8.0 
}

๋„ค ๋ฐฉํ–ฅ์— ๋Œ€ํ•œ inset ๊ฐ’์„ ์ง€์ •ํ•ด์ฃผ๊ณ , ์Šคํ† ๋ฆฌ๋ณด๋“œ์—์„œ๋„ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋„๋ก @IBInspectable์„ ๋ถ™์—ฌ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค! ์„ค์ •ํ•œ inset๋“ค์„ ์ ์šฉํ•˜๋Š” ์‹œ์ ์€ ์–ธ์ œ์ผ๊นŒ์š”?? ์ง๊ด€์ ์œผ๋กœ ์ƒ๊ฐํ•ด๋ณด๋ฉด, UILabel ๋ฐฐ๊ฒฝ์ด ๊ทธ๋ ค์ง€๊ณ , ๊ทธ๋‹ค์Œ์— ํ…์ŠคํŠธ๊ฐ€ ๊ทธ๋ ค์งˆ ํ…๋ฐ ๋”ฑ ๊ทธ ์ ์— ํŒจ๋”ฉ์„ ์ ์šฉํ•ด์„œ ํ…์ŠคํŠธ๋ฅผ ๊ทธ๋ ค์ฃผ๋ฉด ๋˜์ง€ ์•Š์„๊นŒ์š”..! ๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” UILabel์˜ drawText ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•ด์„œ ์žฌ์ •์˜ํ•ด์ค๋‹ˆ๋‹ค.

 

class PaddingLabel: UILabel { 
	@IBInspectable var topInset: CGFloat = 4.0 
	@IBInspectable var bottomInset: CGFloat = 4.0 
	@IBInspectable var leftInset: CGFloat = 8.0 
	@IBInspectable var rightInset: CGFloat = 8.0 

	override func drawText(in rect: CGRect) { 
		let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset) 
		super.drawText(in: rect.inset(by: insets)) 
	} 
}


์ž ์ด๋ ‡๊ฒŒ CGRect์— UIEdgeInsets ์„ ์ ์šฉํ•ด์„œ ํŒจ๋”ฉ์ด ์ ์šฉ๋œ ๋ทฐ์— ํ…์ŠคํŠธ๋ฅผ ๊ทธ๋ ค์ค๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์ด๋ ‡๊ฒŒ๋งŒ ํ•˜๊ณ  ์‹คํ–‰ํ•ด๋ณด๋ฉด,

ํ…์ŠคํŠธ๊ฐ€ ์ž˜๋ ค๋ฒ„๋ฆฝ๋‹ˆ๋‹ค. ์ด์œ ๋Š” ๋‹จ์ˆœํ•ฉ๋‹ˆ๋‹ค. UILabel์€ intrinsicContentSize๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ…์ŠคํŠธ์˜ ๋†’์ด์™€ ๊ธธ์ด์— ๋”ฐ๋ผ ๊ฐ€๋กœ ๋„ˆ๋น„๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” ๋”ฐ๋กœ UILabel์˜ ํฌ๊ธฐ๋ฅผ ์ •ํ•ด์ฃผ์ง€ ์•Š์•„๋„ iOS๊ฐ€ ์ž๋™์œผ๋กœ ๋„ˆ๋น„๋ฅผ ์„ค์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ง€๊ธˆ์€ ํ…์ŠคํŠธ์™€ ํ…์ŠคํŠธ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ทฐ์˜ ๊ธธ์ด๊ฐ€ ํŒจ๋”ฉ์œผ๋กœ ์ธํ•ด ๋‹ฌ๋ผ์ ธ๋ฒ„๋ ธ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ UILabel์˜ intrinsicContentSize๋ฅผ ํŒจ๋”ฉ๊นŒ์ง€ ํฌํ•จํ•˜๋Š” ํฌ๊ธฐ๋กœ ๊ฒฐ์ •ํ•ด์ฃผ์–ด์•ผ ์ •์ƒ์ ์œผ๋กœ ๋ ˆ์ด๋ธ”์ด ๋ณด์—ฌ์ง€๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ UILabel์˜ intrinsicContentSize ์—ฐ์‚ฐ ํ”„๋กœํผํ‹ฐ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•ด์„œ ๋ฐ˜ํ™˜ ๊ฐ’์„ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค!

class PaddingLabel: UILabel { 
    @IBInspectable var topInset: CGFloat = 4.0 
    @IBInspectable var bottomInset: CGFloat = 4.0 
    @IBInspectable var leftInset: CGFloat = 8.0 
    @IBInspectable var rightInset: CGFloat = 8.0 
    
    override func drawText(in rect: CGRect) { 
    	let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset) 
        super.drawText(in: rect.inset(by: insets)) 
    } 
 	override var intrinsicContentSize: CGSize { 
        let size = super.intrinsicContentSize 
    	return CGSize(width: size.width + leftInset + rightInset, height: size.height + topInset + bottomInset) 
    } 
}


์„ค์ •ํ–ˆ๋˜ ์ขŒ์šฐ, ์ƒํ•˜ ํŒจ๋”ฉ์„ ๊ธฐ์กด intrinsicContentSize์˜ ๊ฐ€๋กœ์™€ ์„ธ๋กœ ๊ฐ’์— ๋”ํ•ด์„œ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋„๋ก ์˜ค๋ฒ„๋ผ์ด๋“œํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ํŒจ๋”ฉ์ด ํฌํ•จ๋œ bounds๋„ ์„ค์ •ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ

 

class PaddingLabel: UILabel { 
	@IBInspectable var topInset: CGFloat = 8.0 
	@IBInspectable var bottomInset: CGFloat = 8.0 
	@IBInspectable var leftInset: CGFloat = 16.0 
	@IBInspectable var rightInset: CGFloat = 16.0 

	override func drawText(in rect: CGRect) { 
		let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset) 
		super.drawText(in: rect.inset(by: insets)) 
	} 
	override var intrinsicContentSize: CGSize { 
		let size = super.intrinsicContentSize 
		return CGSize(width: size.width + leftInset + rightInset, height: size.height + topInset + bottomInset) 
	} 
	override var bounds: CGRect { 
		didSet { preferredMaxLayoutWidth = bounds.width - (leftInset + rightInset) } 
	} 
}


์ด๋ ‡๊ฒŒ bounds ํ”„๋กœํผํ‹ฐ๋„ ์˜ค๋ฒ„๋ผ์ด๋“œ๋ฅผ ํ•ด์ฃผ๊ณ  bounds ๊ฐ’์ด ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ๊ฐ€๋กœ ๋„ˆ๋น„๋ฅผ ํŒจ๋”ฉ์„ ํฌํ•จํ•˜๋„๋ก ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค!

๊ทธ๋Ÿผ ์‹คํ–‰ํ•ด๋ณผ๊นŒ์š”?

ํŒจ๋”ฉ์ด ์ž˜ ์ ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค~!!

๊ทธ๋ฆฌ๊ณ  IBInspectable์„ ์ง€์ •ํ•ด๋‘์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ ‡๊ฒŒ ์Šคํ† ๋ฆฌ๋ณด๋“œ ์ƒ์—์„œ๋„ ๊ฐ’์„ ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๋ฏธ๋ฆฌ ์ •์˜ํ•ด๋‘๊ณ  ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•˜๊ฒ ์ฃ ? ์ €๋„ ์ž์ฃผ ์ด์šฉํ•˜๊ฒŒ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

 

์ „์ฒด ์ฝ”๋“œ