티스토리 뷰

현재 스크롤 뷰에 포함된 TextEditor에서 줄바꿈이 발생하면 뷰는 커지지만 스크롤이 되지 않아서 자동 스크롤이 되도록 만들어야 한다.

 

 

 

# TextEditor의 최소 높이 설정하기

먼저 알아야 할 것은 List 또는 ScrollView 안에 TextEditor를 넣으면 TextEditor의 높이는 최소가 된다. 그래서 frame으로 minHeight를 설정해야 한다.

 

 

 

# TextEditor 크기를 구하는 방법

아직은 TextEditor의 크기를 얻는 메서드가 제공되지 않는다. 그래서 bacoground에 HStack같이 확장되는 뷰를 넣고 GeometryReader로 HStack의 사이즈를 측정하면 TextEditor의 크기를 얻을 수 있다.

 

다음은 TextEditor의 크기를 구하는 간단한 SwiftUI 예제코드이다.

struct ContentView: View {
    @State var text = ""
    @State var size = CGSize(width: 0.0, height: 0.0)
    
    var body: some View {
        ScrollView {
            VStack {
                Text("Height * Width = \(size.height) * \(size.width)")
                Text("-----")
                ZStack {
                    TextEditor(text: $text)
                        .frame(minHeight: 50)
                        .fixedSize(horizontal: false, vertical: true)
                        .background(GeometryReader { proxy in
                            HStack {}
                                .onAppear { size = proxy.size }
                                .onChange(of: proxy.size) { _ in size = proxy.size }
                        })
                }
                Text("-----")
            }
        }
    }
}

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함