728x90
안녕하세요.
SwiftUI에서 커스텀 폰트를 사용할 때 이상한 점을 발견했습니다.
우선 아래 코드를 살펴볼게요.
This file contains 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 characters
struct ContentView: View { | |
var body: some View { | |
VStack { | |
Text("안녕하세요 저는 남자입니다.") | |
.font(Font.custom("NotoSansKR-Regular", size: 18)) | |
} | |
} | |
} |
커스텀 폰트를 Text 뷰에 적용한 단순한 코드인데, 화면을 터치하면 글자 간격이 갑자기 줄어드는 현상이 발견되었습니다.

이상하죠...?
조사를 해보면서 몇 가지 알아낸 사실이 있습니다.
- 공백이 없는 문자열은 문제없음. -> 즉, 공백의 자간이 줄어드는 것 같음.
- 기본 폰트에서는 문제 없음.
- VStack, Group 등 컨테이너뷰가 감싸고 있는 형태일 때만 재현됨.
정확한 이유는 잘 모르겠지만, SwiftUI -> UIKit으로 렌더링하는 과정에서 발생하는 버그 때문이라는 추측글을 봤습니다.
이 문제를 해결하기 위한 제 나름의 방법을 공유드릴게요.
# 해결방법
뷰에 강제로 자간(tracking)을 설정해 주면 해결되는 것 같아요.
예시 코드를 살펴보면, 아래쪽 Text 뷰에만 아주 작게 tracking 설정을 해줬더니 화면을 터치해도 자간 변화가 없었습니다.
This file contains 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 characters
struct ContentView: View { | |
var body: some View { | |
VStack { | |
Text("안녕하세요 저는 남자입니다.") | |
.font(Font.custom("NotoSansKR-Regular", size: 18)) | |
Text("안녕하세요 저는 남자입니다.") | |
.font(Font.custom("NotoSansKR-Regular", size: 18)) | |
.tracking(0.00000001) // ✅ | |
} | |
} | |
} |

tracking을 0으로 설정하면 이슈가 계속 발생하니 이 점 참고해 주세요.
이번 글은 여기서 마무리.
반응형
'TroubleShooting' 카테고리의 다른 글
[Fastlane] [!] Read-only file system @ dir_s_mkdir - /Desktop (Errno::EROFS) 해결방법 (0) | 2023.05.30 |
---|---|
[SwiftUI] Unknown preview provider 해결방법 (0) | 2023.05.17 |
[CocoaPods] link_stat failed no such file or directory (2) 해결방법 (0) | 2023.05.11 |
[SwiftUI] iOS 15 이하에서 navigationBarHidden이 동작하지 않는 이슈 (0) | 2023.04.11 |
[CocoaPods] [!] An unexpected error occurred: <!DOCTYPE html> (0) | 2023.03.23 |