-
210520 - TILTIL/2021 2021. 5. 21. 00:44
TextView placeholder
TextField는 placeholde를 기본적으로 제공해주는데 TextView는 직접 구현을 해야 한다.
물론 저번에 구현한 거 같은데 다시 하려고 보니 까먹었으니 다시 공부했다.
간단한 placeholder This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersextension ViewController: UITextViewDelegate { func textViewDidBeginEditing(_ textView: UITextView) { if textView.textColor == .lightGray { textView.text = nil textView.textColor = .black } } func textViewDidEndEditing(_ textView: UITextView) { if textView.text.isEmpty { textView.textColor = .lightGray textView.text = "이곳에 내용을 작성해주세요." } } } 몇 가지 방법이 있겠지만 간단하게 구현하는 방법은 Delegate를 이용하는 방법이다.
이렇게 textViewDidBeginEditing(_:) 과 textViewDidEndEditing(_:) 에서 상황에 맞게 구현하는 방법이다.
TextView(_:shouldChangeTextIn:replacementText:) 에서 현재 텍스트 가져오기
이건 TextField에서 공부했던 내용인데 TextView에서도 다를 게 없으니 구현해봤다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersextension TextViewViewController: UITextViewDelegate { func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { // 현재 텍스트 가져오기 let inputStr = NSString(string: textView.text) let finalStr = inputStr.replacingCharacters(in: range, with: text) print(inputStr) print(finalStr) print("++++++++++++++") return true } } range가 NSRange라 NSString으로 변환시켜야 좀 더 사용하기 편하다.
LocationManager
오늘 가장 중점적으로 공부한 부분이다.
위치 추적 및 현재 위치로 이동 자세히 쓰면 길어지니 핵심적인 부분을 기록하자면
먼저 위치정보는 개인정보이기 때문에 함부로 사용할 수 없어서 사용자 동의를 얻어야 하고
세세한 구현은 CLLocationManager을 이용해야 한다.
디바이스가 위치서비스를 제공하는지 확인하고 권한에 따라서 notDetermined라면 권한을 요청해야 한다.
그리고 delegate를 이용하여 권한이 변경될 때, 에러가 발생했을 때 상황을 코드로 처리해주고
위치 정보는 locationManager(_:didupdateLocations:)에서 location이 업데이트될 때마다 파라미터로 제공하는 locations를 통해서 접근이 가능하다.
현재 위치로 이동하는 버튼은 MapView의 기능을 이용하여 구현했다.
728x90'TIL > 2021' 카테고리의 다른 글
210522 - TIL (0) 2021.05.23 210521 - TIL (0) 2021.05.22 210519 - TIL (0) 2021.05.20 210518 - TIL (0) 2021.05.19 210514 - TIL (0) 2021.05.14