Skip to content

Instantly share code, notes, and snippets.

@tera-ny
Created September 24, 2020 14:17
Show Gist options
  • Save tera-ny/af012ffaef13c88b239233a438384d9b to your computer and use it in GitHub Desktop.
Save tera-ny/af012ffaef13c88b239233a438384d9b to your computer and use it in GitHub Desktop.
How to create a TextView with a ScrollView as a parent in SwiftUI.

what is this?

How to display it in a ScrollView, along with Image and Text.

Turning off the isScrolling property will result in unpredictable results at the time of rendering, so you must leave it turned on and use the TextView. It used mockView to calculate the size of the fit and give it a height with a margin (+10 CGFloat) added.

*It is bad code

If you know of a better way, I'd be very happy to let me know.

import SwiftUI
struct ContentView: View {
var body: some View {
GeometryReader { proxy in
ScrollView(content: {
VStack {
Text("lorem ipsum").bold().font(.title2)
TextView(text: """
https://ja.wikipedia.org/wiki/Lorem_ipsum
lorem ipsum(ロレム・イプサム、略してリプサム lipsum ともいう)とは、出版、ウェブデザイン、グラフィックデザインなどの諸分野において使用されている典型的なダミーテキスト。書籍やウェブページや広告などのデザインのプロトタイプを制作したり顧客にプレゼンテーションしたりする際に、まだ正式な文章の出来上がっていないテキスト部分の書体(フォント)、タイポグラフィ、レイアウトなどといった視覚的なデザインを調整したりわかりやすく見せるために用いられる。
""")
.setFitHeight(width: proxy.size.width)
Text("グリーキング").bold().font(.title2)
TextView(text: """
文書のレイアウトを決める際、テキストの入る部分はべた塗りや記号にするよりは実際の出来上がりに近いフォントによる文章を入れた方が完成時の姿を想像しやすい。しかし一方で、文章が入ると文書全体のデザインよりも文章の内容の方に意識が集中してしまう。そこで欧米などの出版業界やデザイン業界ではタイポグラフィやレイアウトにプレゼンテーションの焦点を当てるため、意味の全くない文字の羅列をテキスト部分に流し込む。
こういった意味不明のダミーテキストを、ギリシャ語(英語圏などでは意味のわからない文章の典型としてギリシャ語が引き合いに出される)にちなみ「グリーキング(英語版)」という[1]。「lorem ipsum」は、英米の出版業界で1960年代かそれ以前の活版印刷の時代から使われた[2]典型的なグリーキング用の文字列であるが、これはギリシア語ではなくラテン語がもとである。「lorem ipsum」は古典ラテン語に非常によく似ているが、実際には全く意味を持たないように作られている[2]。
このテキストが現在の形になったのはいつであるかは正確にはわからないが、おそくとも1960年代と考えられている。特にイギリスのLetraset社の書体見本の広告で長年使われ有名になった[2]。今日の一般的なバージョンは、アルダス社が1980年代半ばにMacintosh向けに開発した史上初のDTPソフト「Aldus PageMaker」(現在の「Adobe PageMaker」)のために作られた[2]。アートディレクターのローラ・ペリー(Laura Perry)はタイポグラフィの見本から古い形の「lorem ipsum」テキストを取り出してソフトに使用されるダミーテキストに採用した[2]。この「lorem」テキストは現在もPageMakerのテンプレートに使用されている。
""")
.setFitHeight(width: proxy.size.width)
}
})
}
}
}
struct TextView: UIViewRepresentable {
let text: String
private static var textAttributes: [NSAttributedString.Key: Any] {
let paragraph = NSMutableParagraphStyle()
paragraph.lineSpacing = 10
return [.font: UIFont.systemFont(ofSize: 20), NSAttributedString.Key.paragraphStyle: paragraph]
}
private static var initialView: UITextView {
let textView = UITextView()
textView.isScrollEnabled = true
textView.isEditable = false
textView.isSelectable = true
textView.dataDetectorTypes = .link
textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.blue]
return textView
}
func makeUIView(context: Context) -> UITextView {
return TextView.initialView
}
func updateUIView(_ uiView: UITextView, context: Context) {
let paragraph = NSMutableParagraphStyle()
paragraph.lineSpacing = 10
uiView.attributedText = NSAttributedString(string: text, attributes: TextView.textAttributes)
}
func setFitHeight(width: CGFloat) -> some View {
let mockView = TextView.initialView
mockView.attributedText = NSAttributedString(string: text, attributes: TextView.textAttributes)
let fitSize = mockView.sizeThatFits(CGSize(width: width, height: .greatestFiniteMagnitude))
return self.frame(width: fitSize.width, height: fitSize.height + 10)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment