mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-24 02:22:44 +02:00
Merge branch 'gil/SP1_space_creation' into gil/5231_SP3-1_Update_room_settings_for_Spaces
# Conflicts: # Podfile.lock
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// Copyright 2021 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
@available(iOS, introduced: 14.0, deprecated: 15.0, message: "Use Text with an AttributedString instead that includes a link and handle the tap by adding an OpenURLAction to the environment.")
|
||||
/// A `Button`, that fakes having a tappable string inside of a regular string.
|
||||
struct InlineTextButton: View {
|
||||
|
||||
private struct StringComponent {
|
||||
let string: Substring
|
||||
let isTinted: Bool
|
||||
}
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
// MARK: Private
|
||||
|
||||
/// The individual components of the string.
|
||||
private let components: [StringComponent]
|
||||
private let action: () -> Void
|
||||
|
||||
|
||||
// MARK: - Setup
|
||||
|
||||
/// Creates a new `InlineTextButton`.
|
||||
/// - Parameters:
|
||||
/// - mainText: The main text that shouldn't appear tappable. This must contain a single `%@` placeholder somewhere within.
|
||||
/// - tappableText: The tappable text that will be substituted into the `%@` placeholder.
|
||||
/// - action: The action to perform when tapping the button.
|
||||
internal init(_ mainText: String, tappableText: String, action: @escaping () -> Void) {
|
||||
guard let range = mainText.range(of: "%@") else {
|
||||
self.components = [StringComponent(string: Substring(mainText), isTinted: false)]
|
||||
self.action = action
|
||||
return
|
||||
}
|
||||
|
||||
let firstComponent = StringComponent(string: mainText[..<range.lowerBound], isTinted: false)
|
||||
let middleComponent = StringComponent(string: Substring(tappableText), isTinted: true)
|
||||
let lastComponent = StringComponent(string: mainText[range.upperBound...], isTinted: false)
|
||||
|
||||
self.components = [firstComponent, middleComponent, lastComponent]
|
||||
self.action = action
|
||||
}
|
||||
|
||||
// MARK: - Views
|
||||
|
||||
var body: some View {
|
||||
Button(action: action) {
|
||||
EmptyView()
|
||||
}
|
||||
.buttonStyle(Style(components: components))
|
||||
.accessibilityLabel(components.map { $0.string }.joined())
|
||||
}
|
||||
|
||||
private struct Style: ButtonStyle {
|
||||
let components: [StringComponent]
|
||||
|
||||
func makeBody(configuration: Configuration) -> some View {
|
||||
components.reduce(Text("")) { lastValue, component in
|
||||
lastValue + Text(component.string)
|
||||
.foregroundColor(component.isTinted ? .accentColor.opacity(configuration.isPressed ? 0.2 : 1) : nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
struct Previews_InlineButtonText_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
InlineTextButton("Hello there this is a sentence. %@.",
|
||||
tappableText: "And this is a button",
|
||||
action: { })
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
//
|
||||
// Copyright 2021 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import DesignKit
|
||||
|
||||
@available(iOS, introduced: 14.0, deprecated: 15.0, message: "Use Text with an AttributedString instead.")
|
||||
/// A `Text` view that renders attributed strings with their `.font` and `.foregroundColor` attributes.
|
||||
/// This view is a workaround for iOS 13/14 not supporting `AttributedString`.
|
||||
struct StyledText: View {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
// MARK: Private
|
||||
|
||||
@Environment(\.theme) private var theme
|
||||
|
||||
/// A string with a bold property.
|
||||
private struct StringComponent {
|
||||
let string: String
|
||||
var font: Font? = nil
|
||||
var color: Color? = nil
|
||||
}
|
||||
|
||||
/// Internal representation of the string as composable parts.
|
||||
private let components: [StringComponent]
|
||||
|
||||
// MARK: - Setup
|
||||
|
||||
/// Creates a `StyledText` using the supplied attributed string.
|
||||
/// - Parameter attributedString: The attributed string to display.
|
||||
init(_ attributedString: NSAttributedString) {
|
||||
var components = [StringComponent]()
|
||||
let range = NSRange(location: 0, length: attributedString.length)
|
||||
let string = attributedString.string as NSString
|
||||
|
||||
attributedString.enumerateAttributes(in: range, options: []) { attributes, range, stop in
|
||||
let font = attributes[.font] as? UIFont
|
||||
let color = attributes[.foregroundColor] as? UIColor
|
||||
|
||||
let component = StringComponent(
|
||||
string: string.substring(with: range),
|
||||
font: font.map { Font($0) },
|
||||
color: color.map { Color($0) }
|
||||
)
|
||||
|
||||
components.append(component)
|
||||
}
|
||||
|
||||
self.components = components
|
||||
}
|
||||
|
||||
/// Creates a `StyledText` using a plain string.
|
||||
/// - Parameter string: The plain string to display
|
||||
init(_ string: String) {
|
||||
self.components = [StringComponent(string: string, font: nil)]
|
||||
}
|
||||
|
||||
// MARK: - Views
|
||||
|
||||
var body: some View {
|
||||
components.reduce(Text("")) { lastValue, component in
|
||||
lastValue + Text(component.string)
|
||||
.font(component.font)
|
||||
.foregroundColor(component.color)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
struct StyledText_Previews: PreviewProvider {
|
||||
static func prettyText() -> NSAttributedString {
|
||||
let string = NSMutableAttributedString(string: "T", attributes: [
|
||||
.font: UIFont.boldSystemFont(ofSize: 12),
|
||||
.foregroundColor: UIColor.red
|
||||
])
|
||||
string.append(NSAttributedString(string: "e", attributes: [
|
||||
.font: UIFont.boldSystemFont(ofSize: 14),
|
||||
.foregroundColor: UIColor.orange
|
||||
]))
|
||||
string.append(NSAttributedString(string: "s", attributes: [
|
||||
.font: UIFont.boldSystemFont(ofSize: 13),
|
||||
.foregroundColor: UIColor.yellow
|
||||
]))
|
||||
string.append(NSAttributedString(string: "t", attributes: [
|
||||
.font: UIFont.boldSystemFont(ofSize: 15),
|
||||
.foregroundColor: UIColor.green
|
||||
]))
|
||||
string.append(NSAttributedString(string: "i", attributes: [
|
||||
.font: UIFont.boldSystemFont(ofSize: 11),
|
||||
.foregroundColor: UIColor.cyan
|
||||
]))
|
||||
string.append(NSAttributedString(string: "n", attributes: [
|
||||
.font: UIFont.boldSystemFont(ofSize: 16),
|
||||
.foregroundColor: UIColor.blue
|
||||
]))
|
||||
string.append(NSAttributedString(string: "g", attributes: [
|
||||
.font: UIFont.boldSystemFont(ofSize: 14),
|
||||
.foregroundColor: UIColor.purple
|
||||
]))
|
||||
return string
|
||||
}
|
||||
|
||||
static var previews: some View {
|
||||
VStack(spacing: 8) {
|
||||
StyledText("Hello, World!")
|
||||
StyledText(NSAttributedString(string: "Testing",
|
||||
attributes: [.font: UIFont.boldSystemFont(ofSize: 64)]))
|
||||
StyledText(prettyText())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user