2017-05-11 2 views
0

프로그래밍 방식으로 UITextView에 접미사 ("kr")를 추가하려면 어떻게해야합니까?iOS Swift - UITextView에 편집 할 수없는 접미사를 추가하는 방법

감사합니다.

+1

를 입력 할 때

override func viewDidLoad() { super.viewDidLoad() myTextView.delegate = self } 

을 그리고 다음 텍스트 뷰에 접미사를 추가 :

의 viewDidLoad에서 당신의 텍스트 뷰 대리자를 지정 (또는 대리자를 할당 할 때마다) textview에 "kr"을 추가 하시겠습니까? –

+0

텍스트보기의 각 단어에 단어를 추가 하시겠습니까? –

+0

이 게시물을 사용하십시오. http://stackoverflow.com/a/8288305/6846532 –

답변

2

사용자가 통화 값을 나타 내기 위해 입력하는 것으로 가정합니다. 당신이 원하는 경우 사용자가

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { 

    if string.characters.count > 0 { 
     amountTypedString += string 
     let newString = amountTypedString + "kr" 
     myTextView.text = newString 
    } else { 
     amountTypedString = String(amountTypedString.characters.dropLast()) 
     if amountTypedString.characters.count > 0 { 
      let newString = amountTypedString + "kr" 
      myTextView.text = newString 
     } else { 
      myTextView.text = "0kr" 
     } 
    } 
    return false 
} 
+0

if string.characters.count> 0 {'는'if! string.isEmpty { 'if에 적용됩니다! if! amountTypedString.isEmpty {' –

관련 문제