티스토리 뷰

iOS/UIKit

[UIKit] UITextField + Inset

SeokBA 2021. 6. 1. 21:30

class InsetTextField: UITextField {
private let commonInsets = UIEdgeInsets(top: 10, left: 15, bottom: 10, right: 15)
private let clearButtonOffset: CGFloat = 5
private let clearButtonLeftPadding: CGFloat = 5
override open func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: commonInsets)
}
override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: commonInsets)
}
// clearButton의 위치와 크기를 고려해 inset을 삽입
override open func editingRect(forBounds bounds: CGRect) -> CGRect {
let clearButtonWidth = clearButtonRect(forBounds: bounds).width
let editingInsets = UIEdgeInsets(
top: commonInsets.top,
left: commonInsets.left,
bottom: commonInsets.bottom,
right: clearButtonWidth + clearButtonOffset + clearButtonLeftPadding
)
return bounds.inset(by: editingInsets)
}
// clearButtonOffset만큼 x축 이동
override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
var clearButtonRect = super.clearButtonRect(forBounds: bounds)
clearButtonRect.origin.x -= clearButtonOffset;
return clearButtonRect
}
}

 

'iOS > UIKit' 카테고리의 다른 글

[UIKit] TableView Cell 개수만큼 Separator line 표시  (0) 2021.06.10
[UIKit] SwiftUI Preview 사용 방법  (0) 2021.06.10
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함