-
Notifications
You must be signed in to change notification settings - Fork 177
[FluentNotification] Add Expand button behavior #2260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nodes11
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
nodes11:dismiss_button_override
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
140 changes: 140 additions & 0 deletions
140
Sources/FluentUI_iOS/Components/ExpandableText/ExpandableText.swift
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| // | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
| // | ||
|
|
||
| import SwiftUI | ||
|
|
||
| /// A text view that automatically detects truncation and supports expand/collapse functionality. | ||
| /// | ||
| /// This component measures the rendered text height and determines if truncation is occurring | ||
| /// based on the specified line limit. It provides an optional callback when expandability changes, | ||
| /// allowing parent components to react (e.g., show/hide an expand button). | ||
| /// | ||
| public struct ExpandableText: View { | ||
| // MARK: - Public Initializers | ||
|
|
||
| /// Creates an expandable text view with plain text. | ||
| /// - Parameters: | ||
| /// - text: The text to display. | ||
| /// - lineLimit: Maximum number of lines to show when collapsed. Default is 2. | ||
| /// - isExpanded: Optional binding to control expansion state externally. | ||
| /// - font: The font to use for measurement and display. If nil, uses system body font. | ||
| /// - onExpandabilityChange: Callback invoked when the expandability status changes. | ||
| public init( | ||
| _ text: String, | ||
| lineLimit: Int = 0, | ||
| isExpanded: Binding<Bool>? = nil, | ||
| font: UIFont? = nil, | ||
| onExpandabilityChange: ((Bool) -> Void)? = nil | ||
| ) { | ||
| self.text = text | ||
| self.attributedText = nil | ||
| self.lineLimit = lineLimit | ||
| self.externalIsExpanded = isExpanded | ||
| self.font = font | ||
| self.onExpandabilityChange = onExpandabilityChange | ||
| } | ||
|
|
||
| /// Creates an expandable text view with attributed text. | ||
| /// - Parameters: | ||
| /// - attributedText: The attributed text to display. | ||
| /// - lineLimit: Maximum number of lines to show when collapsed. Default is 2. | ||
| /// - isExpanded: Optional binding to control expansion state externally. | ||
| /// - onExpandabilityChange: Callback invoked when the expandability status changes. | ||
| public init( | ||
| _ attributedText: NSAttributedString, | ||
| lineLimit: Int = 0, | ||
| isExpanded: Binding<Bool>? = nil, | ||
| onExpandabilityChange: ((Bool) -> Void)? = nil | ||
| ) { | ||
| self.text = attributedText.string | ||
| self.attributedText = attributedText | ||
| self.lineLimit = lineLimit | ||
| self.externalIsExpanded = isExpanded | ||
| self.font = attributedText.attribute(.font, at: 0, effectiveRange: nil) as? UIFont | ||
| self.onExpandabilityChange = onExpandabilityChange | ||
| } | ||
|
|
||
| // MARK: - Public Properties | ||
|
|
||
| public var body: some View { | ||
| Group { | ||
| if let attributed = attributedText { | ||
| Text(AttributedString(attributed)) | ||
| } else { | ||
| Text(text) | ||
| .font(font.map { Font($0) } ?? .body) | ||
| } | ||
| } | ||
| .lineLimit(isExpanded ? nil : (lineLimit > 0 ? lineLimit : nil)) | ||
| .fixedSize(horizontal: false, vertical: true) | ||
| .onGeometryChange(for: CGFloat.self) { geo in | ||
| geo.size.width | ||
| } action: { width in | ||
| calculateTruncation(availableWidth: width) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Private Properties | ||
|
|
||
| private let text: String | ||
| private let attributedText: NSAttributedString? | ||
| private let lineLimit: Int | ||
| private let font: UIFont? | ||
| private let onExpandabilityChange: ((Bool) -> Void)? | ||
|
|
||
| @State private var isExpandable: Bool = false | ||
| @State private var internalIsExpanded: Bool = false | ||
| @State private var availableWidth: CGFloat = 0 | ||
|
|
||
| private var externalIsExpanded: Binding<Bool>? | ||
|
|
||
| /// The current expansion state, reading from external binding if provided, otherwise using internal state. | ||
| private var isExpanded: Bool { | ||
| get { externalIsExpanded?.wrappedValue ?? internalIsExpanded } | ||
| nonmutating set { | ||
| if let binding = externalIsExpanded { | ||
| binding.wrappedValue = newValue | ||
| } else { | ||
| internalIsExpanded = newValue | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// The font to use for rendering and measurements, with fallback to body style. | ||
| private var effectiveFont: UIFont { | ||
| font ?? UIFont.preferredFont(forTextStyle: .body) | ||
| } | ||
|
|
||
| /// The height of a single line of text using the effective font. | ||
| private var singleLineHeight: CGFloat { | ||
| ceil(effectiveFont.lineHeight + max(0, effectiveFont.leading)) | ||
| } | ||
|
|
||
| // MARK: - Private Methods | ||
|
|
||
| /// Calculates whether the text would be truncated by comparing the full text height to the maximum allowed height. | ||
| /// - Parameter availableWidth: The available width for rendering the text. | ||
| private func calculateTruncation(availableWidth: CGFloat) { | ||
|
joannaquu marked this conversation as resolved.
|
||
| guard availableWidth > 0 else { return } | ||
|
|
||
| let messageText: String | ||
| if let attributedText { | ||
| messageText = attributedText.string | ||
| } else { | ||
| messageText = text | ||
| } | ||
| // Calculate the full height the text would take without line limit | ||
| let fullHeight = messageText.preferredSize( | ||
| for: effectiveFont, | ||
| width: availableWidth, | ||
| numberOfLines: 0 | ||
| ).height | ||
| let maxHeight = singleLineHeight * CGFloat(lineLimit < 1 ? Int.max : lineLimit) | ||
|
|
||
| let newExpandable = fullHeight > maxHeight | ||
| isExpandable = newExpandable | ||
| onExpandabilityChange?(newExpandable) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: enableExpandableMessageText