Created
November 22, 2020 17:27
-
-
Save xzoky/bd81f1fda7d3a6702b27b29f2d51e01f to your computer and use it in GitHub Desktop.
MoraView
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
import SwiftUI | |
extension String { | |
var hiraganaMorae: [String] { | |
let diphthongSmallKana = Set(["ゃ", "ゅ", "ょ"].map({ Character($0) })) | |
var output: [String] = [] | |
self.forEach() { character in | |
if (diphthongSmallKana.contains(character)) { | |
output.append((output.popLast() ?? "").appending(String(character))) | |
} else { | |
output.append(String(character)) | |
} | |
} | |
return output | |
} | |
} | |
struct MoraView: View { | |
let text: String | |
private let characterWidth: CGFloat = 25.0 | |
private let digraphSqueezeFactor: CGFloat = 0.7 | |
private let digraphHorizontalOffset: CGFloat = 3.0 | |
var body: some View { | |
HStack(spacing: 1) { | |
ForEach(Array(text.hiraganaMorae.enumerated()), id: \.offset) { (_, mora) in | |
Text(mora) | |
.scaleEffect(x: (mora.count > 1 ? digraphSqueezeFactor : 1)) | |
.transformEffect(CGAffineTransform(translationX: (mora.count > 1 ? digraphHorizontalOffset : 0), y: 0)) | |
.fixedSize(horizontal: true, vertical: false) | |
.frame(width: characterWidth) | |
} | |
} | |
} | |
} | |
// Preview this in a playground, see how you like it 🙂 | |
VStack() { | |
MoraView(text: "にっぽん") | |
MoraView(text: "ざんぎょう") | |
}.padding() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment