Skip to content

Commit ec47d91

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

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
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 & 3 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,14 +405,15 @@ 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)
410412
}
411413
if let annotatedText = options.annotatedText, let onTextLayout = options.onTextLayout {
412414
androidx.compose.material3.Text(text: annotatedText, modifier: options.modifier, color: options.color, fontSize: options.fontSize, fontStyle: options.fontStyle, fontWeight: options.fontWeight, fontFamily: options.fontFamily, letterSpacing: options.letterSpacing, textDecoration: options.textDecoration, textAlign: options.textAlign, lineHeight: options.lineHeight, overflow: options.overflow, softWrap: options.softWrap, maxLines: options.maxLines, minLines: options.minLines, onTextLayout: onTextLayout, style: options.style)
413415
} else {
416+
logger.info("text \(options.text) reservesSpace \(reservesSpace) maxLines \(maxLines) minLines \(minLines)")
414417
androidx.compose.material3.Text(text: options.text ?? "", modifier: options.modifier, color: options.color, fontSize: options.fontSize, fontStyle: options.fontStyle, fontWeight: options.fontWeight, fontFamily: options.fontFamily, letterSpacing: options.letterSpacing, textDecoration: options.textDecoration, textAlign: options.textAlign, lineHeight: options.lineHeight, overflow: options.overflow, softWrap: options.softWrap, maxLines: options.maxLines, minLines: options.minLines, onTextLayout: options.onTextLayout, style: options.style)
415418
}
416419
}
@@ -649,9 +652,13 @@ extension View {
649652
return self
650653
}
651654

652-
@available(*, unavailable)
655+
// SKIP @bridge
653656
public func lineLimit(_ limit: Int, reservesSpace: Bool) -> some View {
657+
#if SKIP
658+
return environment(\.lineLimit, limit).environment(\.lineLimitReservesSpace, reservesSpace)
659+
#else
654660
return self
661+
#endif
655662
}
656663

657664
@available(*, unavailable)

0 commit comments

Comments
 (0)