Skip to content

Commit 8cd4871

Browse files
committed
Add support for lineLimit(_ limit: Int, reservesSpace: Bool)
1 parent 21d428c commit 8cd4871

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

Sources/SkipUI/SkipUI/Environment/EnvironmentValues.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ extension EnvironmentValues {
282282
set { setBuiltinValue(key: "lineLimit", value: newValue, defaultValue: { nil }) }
283283
}
284284

285+
public var lineLimitReservesSpace: Bool? {
286+
get { builtinValue(key: "lineLimitReservesSpace", defaultValue: { nil }) as! Bool? }
287+
set { setBuiltinValue(key: "lineLimitReservesSpace", value: newValue, defaultValue: { nil }) }
288+
}
289+
285290
public var locale: Locale {
286291
get { Locale(LocalConfiguration.current.locales[0]) }
287292
set {

Sources/SkipUI/SkipUI/Text/Text.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ struct _Text: View, Equatable {
364364
let textDecoration = textEnvironment.textDecoration
365365
let textAlign = EnvironmentValues.shared.multilineTextAlignment.asTextAlign()
366366
let maxLines = max(1, EnvironmentValues.shared.lineLimit ?? Int.MAX_VALUE)
367+
let reservesSpace = EnvironmentValues.shared.lineLimitReservesSpace ?? false
368+
let minLines = reservesSpace ? maxLines : 1
367369
let redaction = EnvironmentValues.shared.redactionReasons
368370
let styleInfo = Text.styleInfo(textEnvironment: textEnvironment, redaction: redaction, context: context)
369371
let animatable = styleInfo.style.asAnimatable(context: context)
@@ -390,7 +392,7 @@ struct _Text: View, Equatable {
390392
}
391393
}
392394
}
393-
options = Material3TextOptions(annotatedText: annotatedText, modifier: modifier, color: styleInfo.color ?? androidx.compose.ui.graphics.Color.Unspecified, maxLines: maxLines, style: animatable.value, textDecoration: textDecoration, textAlign: textAlign, onTextLayout: { layoutResult.value = $0 })
395+
options = Material3TextOptions(annotatedText: annotatedText, modifier: modifier, color: styleInfo.color ?? androidx.compose.ui.graphics.Color.Unspecified, maxLines: maxLines, minLines: minLines, style: animatable.value, textDecoration: textDecoration, textAlign: textAlign, onTextLayout: { layoutResult.value = $0 })
394396
} else {
395397
var text: String
396398
if let interpolations {
@@ -403,7 +405,7 @@ struct _Text: View, Equatable {
403405
} else if styleInfo.isLowercased {
404406
text = text.lowercased()
405407
}
406-
options = Material3TextOptions(text: text, modifier: context.modifier, color: styleInfo.color ?? androidx.compose.ui.graphics.Color.Unspecified, maxLines: maxLines, style: animatable.value, textDecoration: textDecoration, textAlign: textAlign)
408+
options = Material3TextOptions(text: text, modifier: context.modifier, color: styleInfo.color ?? androidx.compose.ui.graphics.Color.Unspecified, maxLines: maxLines, minLines: minLines, style: animatable.value, textDecoration: textDecoration, textAlign: textAlign)
407409
}
408410
if let updateOptions = EnvironmentValues.shared._material3Text {
409411
options = updateOptions(options)
@@ -649,9 +651,13 @@ extension View {
649651
return self
650652
}
651653

652-
@available(*, unavailable)
653-
public func lineLimit(_ limit: Int, reservesSpace: Bool) -> some View {
654+
// SKIP @bridge
655+
public func lineLimit(_ limit: Int, reservesSpace: Bool) -> any View {
656+
#if SKIP
657+
return environment(\.lineLimit, limit).environment(\.lineLimitReservesSpace, reservesSpace)
658+
#else
654659
return self
660+
#endif
655661
}
656662

657663
@available(*, unavailable)

0 commit comments

Comments
 (0)