보통 모바일에서 화면이나 텍스트를 꾹 누르면
복사 / 붙여넣기 등의 기능이 있는 메뉴 바가 나오는데,
이를 Edit Menu라고 한다.
Edit Menu


Edit menus | Apple Developer Documentation
An edit menu lets people make changes to selected content in the current view, in addition to offering related commands like Copy, Select, Translate, and Look Up.
developer.apple.com
Edit menu를 통해 사용자는 선택된 컨텐츠로부터 복사, 붙여넣기, 번역, 검색 등을 수행할 수 있다.
시스템이 제공하는 Edit menu 기능들은 UIResponderStandardEditActions 에서 확인할 수 있으며,
Human Interface Guide에서는 시스템이 제공하는 edit menu 사용을 권장한다고 한다.
edit menu가 불릴때, canPerformAction(_:withSender:) 메서드가 호출된다.
canPerformAction(_:withSender:)
canPerformAction(_:withSender:) | Apple Developer Documentation
Requests the receiving responder to enable or disable the specified command in the user interface.
developer.apple.com
- UIResponder의 메서드로, Responder를 상속받는 TextField도 이 메서드를 호출한다.
- edit menu에 어떤 기능을 활성화/비활성화 할 지 여기서 결정한다.
따라서 붙여넣기 기능을 막으려면 여기서 구현해야함!
func canPerformAction(
_ action: Selector,
withSender sender: Any?
) -> Bool
여기서 action은 Selector 타입으로,
edit menu에 들어갈 기능들이 모두 action에 해당한다.
UIResponderStandardEditActions
UIResponderStandardEditActions | Apple Developer Documentation
A set of standard methods that apps can adopt to support editing.
developer.apple.com
- UIResponderStandardEditActions.paste(_:) → 붙여넣기
- UIResponderStandardEditActions.copy(_:) → 복사
- UIResponderStandardEditActions.cut(_:) → 자르기
등등 여러가지 메서드가 존재함.
구현
TextField의 canPerformAction에서 빼고 싶은 action을 조건문으로 체크해서
false를 반환하면 우리가 원하는 기능을 edit menu에서 제거할 수 있게된다.
이를 위해 UITextField를 상속하는 CustomTextField를 선언하고
canPerformAction 메서드를 오버라이딩해서 구현해야 한다.
import UIKit
final class CustomTextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) ||
action == #selector(UIResponderStandardEditActions.cut(_:)) ||
action == #selector(UIResponderStandardEditActions.copy(_:))
{
return false
}
return super.canPerformAction(action, withSender: sender)
}
}

끝.
'iOS' 카테고리의 다른 글
(Swift) Result 타입에 대한 값 맵핑 하기 (0) | 2023.09.27 |
---|
보통 모바일에서 화면이나 텍스트를 꾹 누르면
복사 / 붙여넣기 등의 기능이 있는 메뉴 바가 나오는데,
이를 Edit Menu라고 한다.
Edit Menu


Edit menus | Apple Developer Documentation
An edit menu lets people make changes to selected content in the current view, in addition to offering related commands like Copy, Select, Translate, and Look Up.
developer.apple.com
Edit menu를 통해 사용자는 선택된 컨텐츠로부터 복사, 붙여넣기, 번역, 검색 등을 수행할 수 있다.
시스템이 제공하는 Edit menu 기능들은 UIResponderStandardEditActions 에서 확인할 수 있으며,
Human Interface Guide에서는 시스템이 제공하는 edit menu 사용을 권장한다고 한다.
edit menu가 불릴때, canPerformAction(_:withSender:) 메서드가 호출된다.
canPerformAction(_:withSender:)
canPerformAction(_:withSender:) | Apple Developer Documentation
Requests the receiving responder to enable or disable the specified command in the user interface.
developer.apple.com
- UIResponder의 메서드로, Responder를 상속받는 TextField도 이 메서드를 호출한다.
- edit menu에 어떤 기능을 활성화/비활성화 할 지 여기서 결정한다.
따라서 붙여넣기 기능을 막으려면 여기서 구현해야함!
func canPerformAction(
_ action: Selector,
withSender sender: Any?
) -> Bool
여기서 action은 Selector 타입으로,
edit menu에 들어갈 기능들이 모두 action에 해당한다.
UIResponderStandardEditActions
UIResponderStandardEditActions | Apple Developer Documentation
A set of standard methods that apps can adopt to support editing.
developer.apple.com
- UIResponderStandardEditActions.paste(_:) → 붙여넣기
- UIResponderStandardEditActions.copy(_:) → 복사
- UIResponderStandardEditActions.cut(_:) → 자르기
등등 여러가지 메서드가 존재함.
구현
TextField의 canPerformAction에서 빼고 싶은 action을 조건문으로 체크해서
false를 반환하면 우리가 원하는 기능을 edit menu에서 제거할 수 있게된다.
이를 위해 UITextField를 상속하는 CustomTextField를 선언하고
canPerformAction 메서드를 오버라이딩해서 구현해야 한다.
import UIKit
final class CustomTextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) ||
action == #selector(UIResponderStandardEditActions.cut(_:)) ||
action == #selector(UIResponderStandardEditActions.copy(_:))
{
return false
}
return super.canPerformAction(action, withSender: sender)
}
}

끝.
'iOS' 카테고리의 다른 글
(Swift) Result 타입에 대한 값 맵핑 하기 (0) | 2023.09.27 |
---|