Skip to content

Instantly share code, notes, and snippets.

@dennda
Created December 18, 2018 20:28
Show Gist options
  • Save dennda/a8de8a3eec61f1e003feaea106768129 to your computer and use it in GitHub Desktop.
Save dennda/a8de8a3eec61f1e003feaea106768129 to your computer and use it in GitHub Desktop.
diff --git a/DuolingoMobile/Sources/Library/UI/Views/SpeakableLabel.swift b/DuolingoMobile/Sources/Library/UI/Views/SpeakableLabel.swift
index 0e968a1a4b..1759777630 100644
--- a/DuolingoMobile/Sources/Library/UI/Views/SpeakableLabel.swift
+++ b/DuolingoMobile/Sources/Library/UI/Views/SpeakableLabel.swift
@@ -19,7 +19,7 @@ class SpeakableLabel: UIView {
// If there is a speaker, let the text wrap around it and adjust the line spacing accordingly.
sentenceView.lineSpacing = sentenceView.font.lineHeight * (hasSpeaker ? 0.8 : 0.5)
- sentenceView.wrapView = hasSpeaker ? speakerView : nil
+// sentenceView.wrapView = hasSpeaker ? speakerView : nil
invalidateLayout()
}
@@ -220,7 +220,8 @@ class SpeakableLabel: UIView {
super.layoutSubviews()
if sentenceView.frame.width != sentenceView.preferredMaxLayoutWidth {
sentenceView.preferredMaxLayoutWidth = sentenceView.frame.width
- setNeedsLayout()
+
+ invalidateLayout()
}
}
@@ -248,10 +249,28 @@ class SpeakableLabel: UIView {
override func updateConstraints() {
sentenceView.setContentCompressionResistancePriority(.required, for: .vertical)
sentenceView.snp.remakeConstraints { make in
- make.edges.equalTo(self)
+ if hasSpeaker {
+ let speakerPadding = CGFloat.unit * 0.5
+
+ // top constraint will be determined by speaker.
+ make.bottom.equalTo(self)
+ let scriptDirection = textLanguage.scriptDirection
+ switch scriptDirection {
+ case .LTR:
+ make.left.equalTo(speakerView.snp.right).offset(speakerPadding)
+ make.right.equalToSuperview()
+ case .RTL:
+ make.right.equalTo(speakerView.snp.left).offset(-speakerPadding)
+ make.left.equalToSuperview()
+ }
+ } else {
+ make.edges.equalToSuperview()
+ }
}
speakerView.snp.remakeConstraints { make in
+ make.top.equalToSuperview()
+
let scriptDirection = textLanguage.scriptDirection
switch scriptDirection {
case .LTR:
@@ -260,16 +279,85 @@ class SpeakableLabel: UIView {
make.right.equalTo(self)
}
- // The sentence view should be accounting for the size of the speaker, so top aligning
- // the speaker with the sentence should center it vertically with the first line of text.
- make.top.equalTo(sentenceView)
make.size.equalTo(speakerSize)
+
+ // The speaker's height depends on the font.
+// make.height.equalTo(style.speakerHeight(for: sentenceView.font))
+
+ // Center the speaker vertically with the text…
+ make.centerY.equalTo(sentenceView.snp.top).offset(floor(sentenceView.font.lineHeight / 2))//.priority(.medium)
+
+ // … but make sure it never goes past the top.
+// make.top.greaterThanOrEqualToSuperview()
+
+ // Make the speaker preserve its aspect ratio as its size changes.
+// let speakerSize = speakerView.intrinsicContentSize
+// let aspectRatio = speakerSize.width / speakerSize.height
+// make.width.equalTo(speakerView.snp.height).multipliedBy(aspectRatio)
}
// super is supposed to be called last.
super.updateConstraints()
}
+// override func updateConstraints() {
+// sentenceView.setContentCompressionResistancePriority(.required, for: .vertical)
+//
+// speakerView.snp.remakeConstraints { make in
+// make.top.equalTo(self)
+//
+// let scriptDirection = textLanguage.scriptDirection
+// switch scriptDirection {
+// case .LTR:
+// make.left.equalTo(self)
+// case .RTL:
+// make.right.equalTo(self)
+// }
+//
+// make.size.equalTo(speakerSize)
+// }
+//
+// sentenceView.snp.remakeConstraints { make in
+//
+// let scriptDirection = textLanguage.scriptDirection
+// switch scriptDirection {
+// case .LTR:
+// make.left.equalTo(speakerView.snp.right).offset(.unit * 0.5)
+// make.right.equalToSuperview()
+// case .RTL:
+// make.right.equalTo(speakerView.snp.left).offset(.unit * -0.5)
+// make.left.equalToSuperview()
+// }
+//
+//// make.height.greaterThanOrEqualTo(speakerView)
+// make.centerY.equalTo(speakerView).priority(.medium)
+//
+// make.top.bottom.equalToSuperview()
+// }
+
+// sentenceView.snp.remakeConstraints { make in
+// make.edges.equalTo(self)
+// }
+
+// speakerView.snp.remakeConstraints { make in
+// let scriptDirection = textLanguage.scriptDirection
+// switch scriptDirection {
+// case .LTR:
+// make.left.equalTo(self)
+// case .RTL:
+// make.right.equalTo(self)
+// }
+//
+// // The sentence view should be accounting for the size of the speaker, so top aligning
+// // the speaker with the sentence should center it vertically with the first line of text.
+// make.top.equalTo(sentenceView)
+// make.size.equalTo(speakerSize)
+// }
+//
+// // super is supposed to be called last.
+// super.updateConstraints()
+// }
+
private func updateAlignments() {
sentenceView.textLanguage = textLanguage
sentenceView.textAlignment = textLanguage.textAlignment
diff --git a/DuolingoMobile/Sources/Sessions/Controllers/Challenges/JuicyTranslateChallengeVC.swift b/DuolingoMobile/Sources/Sessions/Controllers/Challenges/JuicyTranslateChallengeVC.swift
index 3d6129fd9d..17827dbb5e 100644
--- a/DuolingoMobile/Sources/Sessions/Controllers/Challenges/JuicyTranslateChallengeVC.swift
+++ b/DuolingoMobile/Sources/Sessions/Controllers/Challenges/JuicyTranslateChallengeVC.swift
@@ -95,9 +95,20 @@ final class JuicyTranslateChallengeVC: UIViewController, DUOChallengeContentView
wordBankView = DUOWordBankView(frame: CGRect.zero)
prompt = SpeakableLabel(textLanguage: element.sourceLanguage)
- prompt.model = DUOHintableString(tokens: element.tokens)
+
+
+// prompt.model = DUOHintableString(tokens: element.tokens)
+
+// let tokens = ["El", " ", "pueblo", " ", "tiene", " ", "dos", " ", "supermercados."]
+// .map({ DUOChallengeToken(explanationId: nil, hintTable: nil, ttsURL: nil, value: $0) })
+// let fakeModel = DUOHintableString(tokens: tokens)
+// prompt.model = fakeModel
+ prompt.model = DUOHintableString(string: "Este pueblo tiene dos supermercados.", language: "es")
+
+
prompt.sentenceAccessibilityLabel = element.sentence
- prompt.hasSpeaker = element.sourceLanguage != element.courseFromLanguage && (element.ttsURL != nil)
+// prompt.hasSpeaker = element.sourceLanguage != element.courseFromLanguage && (element.ttsURL != nil)
+ prompt.hasSpeaker = true
super.init(nibName: nil, bundle: nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment