SwiftUI Text not shrinking with .fixedSize() - swift

I'm working on an app with a piece of large text with buttons underneath which I want to take up the same width as the text. My solution is to use a VStack with the .fixedSize() modifier:
VStack {
Text(viewModel.timeString)
.animation(nil)
.minimumScaleFactor(0.1)
.lineLimit(1)
.font(.system(size: 100, design: .monospaced))
.padding(.vertical, -20)
TimerActionsCompact(viewModel: viewModel) // the 3 buttons
}
.fixedSize()
This produces the result I want:
However, when the text becomes too big, it just goes off the screen instead of shrinking, even though I have minimumScaleFactor and lineLimit set.
I found that the problem was .fixedSize(), and removing it causing the text to shrink properly.
Am I doing something wrong, or could this be a bug with swiftUI? If so is there any workaround?
Thanks!

One possible way you can use frame modifier like this example:
struct ContentView: View {
#State private var string: String = "0.0"
var body: some View {
Text(string)
.font(.system(size: 100, design: .monospaced))
.minimumScaleFactor(0.1)
.lineLimit(1)
.frame(width: 200, height: 80) // give your custom size here as you like!
.background(Color.gray.cornerRadius(10.0))
.animation(nil, value: string)
Button("add") { string += String(describing: Int8.random(in: 0...9)) }.padding()
}
}

Related

How to keep an item exactly in the center of an HStack with other items in the HStack

I have two items in an HStack. I want one of them to be perfectly horizontally centered and the other to be on the left side (have an alignment of .leading). Here is my code so far:
HStack {
Text("1")
.frame(alignment: .leading)
Text("Hello")
.frame(alignment: .center)
}
I have figured out that the .overlay method works. However, I still want Text("Hello") to be aware of the position of Text("1") so that if Text("Hello") were a longer string, it would know not to cover up Text("1"), which is exactly what happens when the .overlay function is used. So I'm wondering if there are any other solutions.
You can add one more Text view inside HStack as hidden which have the same content as your left align text view along with Spacer. Something like this.
HStack {
Text("1")
Spacer()
Text("Good Morning, Hello World. This is long message")
.multilineTextAlignment(.center)
Spacer()
Text("1") // Set the same content as left aligned text view
.hidden()
}
.padding(.horizontal)
Preview
Not sure if it's the best way, but you can use a GeometryReader to get the screen width and skip the first half.
To place the text exactly in the center, you should also calculate the width of the text. you can use a simple extension to get the size of your text:
extension String {
public func size(withFont font: UIFont) -> CGSize {
(self as NSString).size(withAttributes: [.font: font])
}
}
Here is the example code:
GeometryReader { geo in
HStack {
Spacer()
.frame(width: (geo.size.width - "center".size(withFont: UIFont.systemFont(ofSize: 18)).width) / 2)
HStack {
Text("center")
Text("another")
}
}
}

How to align shapes and text accurately in SwiftUI

Is there a way to align object such that they resze according to the unused space?
Here is an example - there are three objects, a background square and foreground text with a highlight rectangle below the text. The text and the highlight should be spaced as though there is an equal amount of space either side (or near enough)...
This is as far as I have got (I have tried a variety of different iterations although the Spacer command seems a little ideosynchratic (depending on a variety of factors I don't entirely understand yet)...
struct hlGroup: View {
var body : some View {
Color(red:20/255,green: 45/255, blue:71/255)
.frame(width:50, height:56)
.cornerRadius(6)
.overlay(alignment:.center){
Spacer()
VStack(){
Text(String(0))
.foregroundColor(.white)
.font(Font.system(size:30, weight:.bold))
HStack(spacing: 2){
hlShow(hlColor: Color(red:100/255, green: 227/255, blue: 50/255))
}
Spacer()
}}}}
struct hlShow: View {
let hlColor :Color
var body: some View {
Rectangle()
.foregroundColor(hlColor)
.frame(width:30, height:6)
.cornerRadius(2)
}}
Unless I'm missing some specific requirement, the layout could be easily achieved using a VStack as the main container (added a border to text acting as a bounds indicator - previewing at 80 × 80):
struct SODemo: View {
var body: some View {
VStack {
Spacer()
Text("Text")
.foregroundColor(.white)
.font(Font.system(size: 30, weight: .bold))
.border(.red)
Spacer()
Color.green.frame(height: 6)
Spacer()
}
.background(Color.blue)
.cornerRadius(6)
}
}

SwiftUI Center Content alignment without supporting frames

I'm having trouble trying to center a single element to emulate the navigation modal with a close button.
I would like to center content without using a supporting Rectangle on the sides or spacers.
What i'm trying to achieve is whenever the text grow, if it reaches the left sides where there is the close xmark button it should try to push itself on the right where there is available space until it reaches the right border and after wrap itself if there are no available space on the both sides.
here are some pictures:
expected result 1
expected result 2
current solution short text
current solution long text
i tried using long and short text to test the content behaviour
Currently this is the start of the code and basically i would like to avoid to add the blue rectangle (that would be usually clear)
struct TestAlignmentSwiftUIView: View {
var body: some View {
HStack(spacing: 0) {
Rectangle().fill(Color.blue).frame(width: 44, height: 44)
Text("aaa eee aaa")
.background(Color.red)
.padding(5)
Button(action: {}, label: {
Image(systemName: "xmark")
.padding(15)
.frame(width: 44, height: 44)
.background(Color.yellow)
})
}
.background(Color.green)
}
}
What i've tried so far but doesn't resolve the issue if the code inside the text component grow:
Using a zstack where i place the text and the close button one on
top of each other but the button is pushed to the side using a spacer. It will work for small text or content but is not scalable if the text grows
var body: some View {
ZStack {
HStack {
Spacer()
Button(action: {}, label: {
Image(systemName: "xmark")
.padding(15)
.frame(width: 44, height: 44)
.background(Color.yellow)
})
}
Text("aaa eee aaa random long very long text that should wrap without overlapping. long text")
.background(Color.red)
.frame(maxWidth: .infinity, alignment: .center)
.padding(5)
.opacity(0.7)
}
.background(Color.green)
}
Using alignment guides :
i would create my own center alignment guide, then use this custom alignment on a vstack where i place my content plus a fake filler rectangle that should center the elements on the content side.
the problem is that with swiftui , as far i know, you can only align one descendant element, and doesn't support multiple custom alignments on the stack of elements. so i would have only the text centered or the side button aligned not both aligned one to the center and the other to the trailing edge. and if i put a spacer between them it will just mess the alignment created. If I try with small text they will be both attached.
Heres the code. try to comment the button and you will see that it will center itself or add spacer between them.
extension HorizontalAlignment {
private enum MyAlignment: AlignmentID {
static func defaultValue(in d: ViewDimensions) -> CGFloat {
d[HorizontalAlignment.center]
}
}
static let myAlignment = HorizontalAlignment(MyAlignment.self)
}
var body: some View {
VStack(alignment: .myAlignment, spacing: 0) {
HStack {
Text("aaa eee aaa random ")
.background(Color.red)
.frame(maxWidth: .infinity, alignment: .center)
.padding(5)
.alignmentGuide(.myhAlignment, computeValue: { dimension in
dimension[HorizontalAlignment.center]
})
Button(action: {}, label: {
Image(systemName: "xmark")
.padding(15)
.frame(width: 44, height: 44)
.background(Color.yellow)
})
}
.background(Color.green)
Rectangle()
.fill(Color.purple)
.frame(width: 10, height: 10, alignment: .center)
.alignmentGuide(.myhAlignment, computeValue: { dimension in
dimension[HorizontalAlignment.center]
})
}
}
Tried with a combination of geometry reader and/or anchor preferences to read with sizes of the text content and side button width and apply the appropriate center offset manually, but it seems too hacky and it never worked as expected without good results
If you're familiar with uikit this problem would be resolved using a
centerX on the container with a minor layout priority and a right constraint from the center to the
close button, and call it a day. But on swiftui it seems soo hard to
handle this simple cases.
So far i haven't found a solution without using a supporting fixed frame on the side that would work with both long and short text. that space is clearly visibile if you try to use long text. and it will leave the user to wonder why is not used.
¯\ (ツ)/¯
EDIT: added possible solution in the answers
From the #Yrb suggestion in the comments, here's what i came up that shrink the blue size so it will center on the available space.
I added a fake text underneath and tracked the size. and if it's over the available space i will take the difference and shrink the blu rectangle.
One thing to keep in mind is that the hidden content if contains some text should have linelimit 1, otherwise it will get a smaller size from wrapping itself.
And i just assume that i know the size of the close button (or at least one side) for center alignment, and even if i don't know it at compile time, i could probably use a preference key to get the size at run time, and have it dynamic.
But for the moment i think it's fine the result that i got.
but honestly i hope to find something more easier in the future.
#State var text: String = "aaa eee aaa"
#State private var fillerWidth: CGFloat = 44
// i assume i know the max size of the close button or at least one side
private let kCloseButtonWidth: CGFloat = 44
private struct FakeSizeTitlteContentKey: PreferenceKey {
static var defaultValue: CGFloat { .zero }
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = nextValue()
}
}
var body: some View {
ZStack(alignment: Alignment(horizontal: .center, vertical: .top)) {
GeometryReader { parentGeometry in
titleContent
.lineLimit(1) // hidden text must not wrap
.overlay(GeometryReader { proxyFake in
Color.clear.border(Color.black, width: 0.3)
.preference(key: FakeSizeTitlteContentKey.self, value: proxyFake.frame(in: .local).width
.onPreferenceChange(FakeSizeTitlteContentKey.self) { value in
let availableW = parentGeometry.frame(in: .local).width
let fillSpace = availableW - value - kCloseButtonWidth * 2
fillerWidth = min(kCloseButtonWidth, max(0, fillSpace))
}
})
}
.hidden()
VStack {
HStack(spacing: 0) {
Rectangle()
.fill(Color.blue)
.frame(width: fillerWidth, height: 44)
titleContent
.background(Color.green)
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity, alignment: .center)
Button(action: {}, label: {
Image(systemName: "xmark")
.padding(15)
.frame(width: kCloseButtonWidth, height: kCloseButtonWidth)
.background(Color.yellow)
})
}
.coordinateSpace(name: "fullCont")
.background(Color.green)
TextEditor(text: $text)
.frame(maxHeight: 150, alignment: .center)
.border(Color.black, width: 1)
.padding(15)
Spacer()
}
}
}
#ViewBuilder var titleContent: some View {
HStack(spacing: 0) {
Text(text)
.background(Color.red)
.padding(.horizontal, 5)
}
}

How to make TextEditor look like TextField - SwiftUI

I'm looking for a way to make a swiftUI TextEditor look like a TextField. I need a multiline input but I really like the appearance of rounded TextFields.
This is the closest I was able to get:
Using this code:
TextEditor(text: $instanceNotes)
.border(Color.gray)
.foregroundColor(.secondary)
.cornerRadius(3)
.padding(.horizontal)
.frame(height:100)
which looks/acts nothing like a TextField
This is the code for the TextField I want to replicate:
TextField("Name", text: $instanceName)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding(.horizontal)
Thanks!
This gets closer (no focus support, although that could be added):
TextEditor(text: $textBinding)
.padding(4)
.overlay(RoundedRectangle(cornerRadius: 8)
.stroke(Color.secondary).opacity(0.5))
I think the most important thing is making sure that the border is actually rounded -- right now, yours is getting cut off at the corners.
How could I change it when it's focused like TextField does?
…
#State private var text: String = ""
#FocusState private var isFocused: Bool
…
…
TextEditor(text: $textBinding)
.focused($isFocused)
.padding(4)
.overlay(RoundedRectangle(cornerRadius: 8)
.stroke(isFocused ? Color.secondary : Color.clear).opacity(0.5))
…
Show a TextEditor more accurately like a TextField:
…
Text(text)
.padding(.vertical, 10) // If not text first line hides under TextField
.padding(.horizontal, 5) // If not TextField can be saw on the right side
.frame(maxWidth: .infinity, // Frame on the screen size
minHeight: 40 // Initial size equivalent to one line
)
.overlay(
TextEditor(text: $text)
.focused($isFocused)
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(isFocused ? Color.secondary : Color.clear).opacity(0.5)
)
)
…
Having the TextEditor in a Form causes the default style to change, so, the accepted answer doesn't work correctly.
I managed to get it working using:
import PlaygroundSupport
import SwiftUI
struct ContentView: View {
#State private var text: String = "Test"
var body: some View {
Form {
TextEditor(text: self.$text)
.background(Color.primary.colorInvert())
.cornerRadius(5)
.overlay(
RoundedRectangle(cornerRadius: 5)
.stroke(.black, lineWidth: 1 / 3)
.opacity(0.3)
)
TextField("", text: self.$text)
.textFieldStyle(.roundedBorder)
}
}
}
PlaygroundPage.current.setLiveView(
VStack {
ContentView()
.environment(\.colorScheme, .light)
ContentView()
.environment(\.colorScheme, .dark)
}
)

How to make a Rectangle() and a DatePicker overlap?

So I'm trying to move the rectangle so that it looks like there's a little point at the bottom of the DatePicker() UI element:
import SwiftUI
struct TimeSelectorView: View {
#State private var selectedDay: Date = Date()
var body: some View {
VStack {
DatePicker(selection: $selectedDay, in: Date()..., displayedComponents: .date) {
Text("")
}.frame(width: UIScreen.main.bounds.width * 0.75)
.clipped()
.background(Color.white)
.cornerRadius(15)
Rectangle()
.fill(Color.white)
.frame(width: 25, height: 25)
.rotationEffect(Angle(degrees: 45))
.padding(.bottom, 15)
}
}
}
To create overlapping Views you can use a ZStack.
SwiftUI has a dedicated stack type for creating overlapping content,
which is useful if you want to place some text over a picture for
example. It’s called ZStack, and it works identically to the other two
stack types [HStack, VStack].
In the below example, the DatePicker will be placed behind the Rectangle:
ZStack {
DatePicker(...)
Rectangle()
}
You can find more information in this tutorial: How to layer views on top of each other using ZStack?