iOS/๐Ÿค– App

[iOS] UITextField ์— ๋ถ™์—ฌ๋„ฃ๊ธฐ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ

woozzang 2022. 11. 3. 23:26

 

์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด์•ผํ•˜๋Š” ํ•„๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

์ด ๋•Œ, ๋ถ™์—ฌ๋„ฃ๊ธฐ๋ฅผ ํ†ตํ•ด ๋ฌธ์ž๊ฐ€ ์ž…๋ ฅ๋˜๋Š” ๊ฒƒ๋„ ๋ฐฉ์ง€ํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค.

 

๋ถ™์—ฌ๋„ฃ๊ธฐ๋กœ ๋ฌธ์ž๊ฐ€ ์ž…๋ ฅ๋œ ์ดํ›„์— ๊ฐ์ง€ํ•ด์„œ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ฒ ์ง€๋งŒ,

๋ถ™์—ฌ๋„ฃ๊ธฐ ์ž์ฒด๋ฅผ ์•„์˜ˆ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค.

 

UITextField ์— ๋ถ™์—ฌ๋„ฃ๊ธฐ๋ฅผ ๋ฐฉ์ง€ํ•ด๋ณด์ž.

 

์‚ฌ์šฉ์ž๋Š” UITextField ๋ฅผ ํƒญํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์€ UI ๋ฅผ ํ™”๋ฉด์— ๋„์šธ ์ˆ˜ ์žˆ๋‹ค.

 

 

์ด๊ฒƒ์„ editing menu ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

๊ทธ๋ฆฌ๊ณ  editing menu ๊ฐ€ ๋“ฑ์žฅํ•˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋‹ค.

canPerformAction(_:withSender:) ์ด๋‹ค.

 

์ด ๋ฉ”์„œ๋“œ๋Š” UIResponder ์— ์ •์˜๋˜์–ด์žˆ๋‹ค.

UITextField ๋Š” UIResponder ๋ฅผ ์ƒ์†ํ•œ ํด๋ž˜์Šค์ด๋ฏ€๋กœ, ์‚ฌ์šฉ์ž๊ฐ€ TextField ๋ฅผ ํƒญํ•  ๋•Œ canPerformAction(_:withSender:)  ๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

 

์‹œ์Šคํ…œ์€ ์ด ๋ฉ”์„œ๋“œ์— action ๋“ค์„ ์ „๋‹ฌํ•˜๊ณ ,

๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ๊ฐ’์œผ๋กœ ์œ„์˜ UI ์— action ์„ ์ถ”๊ฐ€ํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.

 

action ์—๋Š” copy, paste ๋“ฑ์ด ์žˆ๋Š”๋ฐ,

UIStandartEditAction ๋ฌธ์„œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋” ๋‹ค์–‘ํ•œ action ์„ ์‚ดํŽด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

๋‹ค์‹œ ๋Œ์•„์™€์„œ, canPerformAction(_:withSender:) ๊ฐ€

- true ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์‹œ์Šคํ…œ์€ edit menu ์— ํ•ด๋‹น ์•ก์…˜์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

- false ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์‹œ์Šคํ…œ์€ edit menu ์— ํ•ด๋‹น ์•ก์…˜์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

์œ„ ๊ทœ์น™์„ ์ด์šฉํ•ด์„œ ๊ตฌํ˜„ํ•˜๋ฉด,

- ์‹œ์Šคํ…œ์ด canPerformAction(_:withSender:) ์— paste(_:) ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ, false ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฉด ๋  ๊ฒƒ์ด๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด UITextField ์˜ ๋™์ž‘์„ ์œ ์ง€ํ•˜๋ฉด์„œ ์ด ๋ถ€๋ถ„์˜ ๋™์ž‘๋งŒ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ด ์žˆ์„๊นŒ?

๋‚˜๋Š” ์ƒ์†๊ณผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ์„ ์ด์šฉํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค.

๋” ์ข‹์€ ๊ตฌํ˜„๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค๋ฉด ๊ณต์œ ํ•ด์ฃผ์„ธ์š”!

 

import UIKit

final class PasteBlockableTextField: UITextField {
    
    private var _isPasteBlocked: Bool = false
    
    var isPasteBlocked: Bool {
        get {
            self._isPasteBlocked
        }
        set {
            self._isPasteBlocked = newValue
        }
    }
    
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        
        if isPasteBlocked && action == #selector(UIResponderStandardEditActions.paste(_:)) {
            return false
        }
        
        return super.canPerformAction(action, withSender: sender)
    }
}

 

 

 


์˜ค๋Š˜๋„ ๋‚˜๋Š” ์„ฑ์žฅํ–ˆ๋‹ค!!๐Ÿ”ฅ

 

๐Ÿค– [Back to the Basics] ๐Ÿ’ป
์ฃผ๋‹ˆ์–ด iOS ๊ฐœ๋ฐœ์ž ์šฐ์งฑ์˜ ๊ธฐ์ˆ  ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.

 

[์ฐธ๊ณ  ์ž๋ฃŒ ๐Ÿ™‡๐Ÿป‍โ™‚๏ธ]

https://developer.apple.com/documentation/uikit/uiresponder/1621105-canperformaction/

https://developer.apple.com/documentation/uikit/uiresponderstandardeditactions

https://developer.apple.com/documentation/uikit/uiresponderstandardeditactions/2354189-paste