๐ŸŽ Apple/UIKit

textView์—์„œ ํ‚ค๋ณด๋“œ์˜ Enter ํ‚ค๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ viewController dismiss์ฝ”๋“œ

JINiOS 2022. 10. 3. 16:44
728x90

textView์—์„œ๋Š” multi-line์„ ์ž…๋ ฅํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋‹ค์Œ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์—”ํ„ฐ ์ž…๋ ฅ์„ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {       
        guard text == "\n" else { return true }
        dismiss(animated: true)
        return true
 }

 

 

 

๋ฐ˜ํ™˜ ๊ฐ’(Return Value)

true  >>  if the old text should be replaced by the new text 
์ด์ „ ํ…์ŠคํŠธ๋ฅผ ์ƒˆ ํ…์ŠคํŠธ๋กœ ๋Œ€์ฒดํ•ด์•ผ ํ•  ๊ฒฝ์šฐ

 

false >> if the replacement operation should be aborted
๊ต์ฒด๋ฅผ  ์ค‘๋‹จํ•ด์•ผ ํ•  ๊ฒฝ์šฐ

 

๋…ผ์˜(Discussion)

The text view calls this method whenever the user types a new character or deletes an existing character. Implementation of this method is optional. You can use this method to replace text before it is committed to the text view storage. For example, a spell checker might use this method to replace a misspelled word with the correct spelling.

textView๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ƒˆ ๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด ๋ฌธ์ž๋ฅผ ์‚ญ์ œํ•  ๋•Œ๋งˆ๋‹ค textView(shouldChangeText)๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ text view ์ €์žฅ์†Œ์— ์ปค๋ฐ‹ํ•˜๊ธฐ ์ „์— ํ…์ŠคํŠธ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
728x90