Text overflow onto new line with WrappingHStack - swift

I'm using WrappingHStack, and I want text elements within it (on buttons, see below) to overflow onto a new line. Currently, this does not happen — it gets truncated with ... at the end.
This does not happen if the button is outside the WrappingHStack:
Outside of WrappingHStack, text correctly overflows onto new line:
Button(action: {
//
}) {
VStack {
Text("A very long sentence. Hopefully it will flow onto the next line")
.font(.system(size: 18, weight: .bold, design: .rounded))
.foregroundColor(.white)
}
.frame(alignment: .leading)
.padding()
.background(
RoundedRectangle(cornerRadius: 6)
.fill(colours[0])
)
.padding(5)
}
.padding(.bottom, 10)
Inside WrappingHStack, it doesn't:
WrappingHStack(0..<sentences.count, id:\.self, alignment: .center) { i in
Button(action: {
//
}) {
VStack {
Text("A very long sentence. Hopefully it will flow onto the next line")
.font(.system(size: 18, weight: .bold, design: .rounded))
.foregroundColor(.white)
}
.frame(alignment: .leading)
.padding()
.background(
RoundedRectangle(cornerRadius: 6)
.fill(colours[i])
)
.padding(5)
}
.padding(.bottom, 10)
}
How can I ensure text overflows in the WrappingHStack too?

Try the below answer:
add .fixedSize(horizontal: false, vertical: true)
VStack {
Text("A very long sentence. Hopefully it will flow onto the next line")
.font(.system(size: 18, weight: .bold, design: .rounded))
.foregroundColor(.white)
.fixedSize(horizontal: false, vertical: true)
}

Related

Why do the elements inside my VStack inside a HStack still go vertical

So basically this is my code.
Text("Melbourne, Victoria")
.font(.largeTitle)
.fontWeight(.bold)
.foregroundColor(Color.white)
.padding(.bottom, 30)
Image(systemName: "moon.fill")
.foregroundColor(Color.white)
.font(.system(size: 60))
Text("Today")
.font(.title)
.fontWeight(.medium)
.foregroundColor(Color.white)
Text("34°C")
.font(.title3)
.fontWeight(.medium)
.foregroundColor(Color.white)
Spacer()
HStack {
Image(systemName: "sun.max.fill")
.foregroundColor(Color.yellow)
.font(.system(size: 40))
.padding(.bottom, 550)
Text("Mon 34°C")
.font(.title)
.fontWeight(.medium)
.foregroundColor(Color.white)
.padding(.bottom, 550)
}
}
.background(
Image("night")
.ignoresSafeArea()
)
I want to make more of the days in this weather app each with it's on SF Symbol and everything is inside this VStack. And the days and SF symbols are inside a HStack to keep them horizontal. But if I want to put more of those the next time I do it they go next to each other but the symbol goes on top look at this.
This is the image with only 1 of those days
And this one is when I put more than 1 but its next to each other when I want them vertically aligned.
This is the one if I put them in another VStack which makes the SF symbol go above the text
IS there any solution to this?
The problem is your .padding(.bottom, 550).
Image(systemName: "sun.max.fill")
.foregroundColor(Color.yellow)
.font(.system(size: 40))
.padding(.bottom, 550) /// here!
It's currently attached to your first Image. As a result, the HStack is stretched vertically and it appears as though the first Image is higher up than second one.
To fix, move both .padding(.bottom, 550)s to outside your HStack.
You have quite a lot of arbitrary values for padding. You should have your layout adapt to various devices, rather than hard-coding that .bottom spacing for example.
Working example:
struct ContentView: View {
var body: some View {
VStack(spacing: 40) {
Text("Melbourne, Victoria")
.font(.largeTitle)
.fontWeight(.bold)
.foregroundColor(Color.white)
VStack {
Image(systemName: "moon.fill")
.foregroundColor(Color.white)
.font(.system(size: 60))
Text("Today")
.font(.title)
.fontWeight(.medium)
.foregroundColor(Color.white)
Text("34°C")
.font(.title3)
.fontWeight(.medium)
.foregroundColor(Color.white)
}
VStack(spacing: 10) {
HStack {
Image(systemName: "sun.max.fill")
.renderingMode(.original)
.font(.system(size: 40))
Text("Mon 34°C")
.font(.title)
.fontWeight(.medium)
.foregroundColor(Color.white)
}
HStack {
Image(systemName: "cloud.sun.fill")
.renderingMode(.original)
.font(.system(size: 40))
Text("Tue 28°C")
.font(.title)
.fontWeight(.medium)
.foregroundColor(Color.white)
}
}
Spacer()
}
.frame(maxWidth: .infinity)
.background(
Color(red: 0.34, green: 0.36, blue: 0.71)//Image("night")
.ignoresSafeArea()
)
}
}
Result:
Although there is already an outer VStack, they are nested VStacks because they have different amounts of padding. Multiple views are grouped together, and then those are grouped together with larger paddings to separate them.
Bonus
SF Symbols colors
Use:
.renderingMode(.original)
Rather than:
.foregroundColor(Color.yellow)
To color the SF Symbols with the default colors, as shown in the above example.
Align days
You can align the days across each HStack with a custom alignment guide. Use the following:
extension HorizontalAlignment {
struct LeadingDay: AlignmentID {
static func defaultValue(in context: ViewDimensions) -> CGFloat {
context[.leading]
}
}
static let leadingDay = HorizontalAlignment(LeadingDay.self)
}
Making the other following changes:
VStack(alignment: .leadingDay, spacing: 10) { // <- HERE
HStack {
Image(systemName: "sun.max.fill")
.renderingMode(.original)
.font(.system(size: 40))
Text("Mon 34°C")
.font(.title)
.fontWeight(.medium)
.foregroundColor(Color.white)
.alignmentGuide(.leadingDay) { d in // <- HERE
d[.leading]
}
}
HStack {
Image(systemName: "cloud.sun.fill")
.renderingMode(.original)
.font(.system(size: 40))
Text("Tue 28°C")
.font(.title)
.fontWeight(.medium)
.foregroundColor(Color.white)
.alignmentGuide(.leadingDay) { d in // <- HERE
d[.leading]
}
}
}
Result:

NavigationLink Text blurry swiftui

I have a section that I have some items inside and I want those items to trigger separate Views the problem I have is the color contrast on my NavigationLink Button is blurry but if I use a regular button, it come out correct. This is my code and this is the image that shows up
VStack {
Form {
Section {
Button(action: {}, label: {
HStack(spacing: 2) {
Image("logouticon")
.frame(height: 40)
.frame(width: 40)
.foregroundColor(.black)
Text("Logout")
.foregroundColor(.red)
.font(.system(size: 14, weight: .regular, design: .default))
.multilineTextAlignment(.leading)
.foregroundColor(.black)
}.background(Color.clear)
})
NavigationLink(
destination: EmptyView(),
label: {
HStack(spacing: 2) {
Image("logouticon")
.frame(height: 40)
.frame(width: 40)
.foregroundColor(.black)
Text("Logout")
.foregroundColor(.red)
.font(.system(size: 14, weight: .regular, design: .default))
.multilineTextAlignment(.leading)
.foregroundColor(.black)
}
}
)
}
}
}
The image below is the NavigationLink while the one above it is the Button
You need to put all NavigationLinks in a NavigationView like this:
NavigationView {
VStack {
Form {
Section {
Button(action: {}, label: {
HStack(spacing: 2) {
Image("logouticon")
.frame(height: 40)
.frame(width: 40)
.foregroundColor(.black)
Text("Logout")
.foregroundColor(.red)
.font(.system(size: 14, weight: .regular, design: .default))
.multilineTextAlignment(.leading)
.foregroundColor(.black)
}.background(Color.clear)
})
NavigationLink(
destination: EmptyView(),
label: {
HStack(spacing: 2) {
Image("logouticon")
.frame(height: 40)
.frame(width: 40)
.foregroundColor(.black)
Text("Logout")
.foregroundColor(.red)
.font(.system(size: 14, weight: .regular, design: .default))
.multilineTextAlignment(.leading)
.foregroundColor(.black)
}
}
)
}
}
}
}
Otherwise, they are dimmed because they are disabled.

SwiftUI rectangle taking up the wrong amount of space

I made a comment feed for a subreddit I moderate and I wanted to make a vertical line on the left side of the comment underneath the title of the post in SwiftUI. I made the line using a rounded rectangle but the problem is instead of shaping itself to the height of the text, it is a certain height and makes the text conform to that height. Any idea how to fix this? Thanks!
Screenshot
Code
VStack {
HStack {
Text(comment.link_title ?? "")
.font(.callout)
.bold()
.padding(.horizontal, 5)
.foregroundColor(Color("black"))
Spacer()
}
HStack {
RoundedRectangle(cornerRadius: 2, style: .continuous)
.frame(width: 3)
.foregroundColor(Color("medGray"))
.padding(.leading, 10)
Text(comment.body ?? "")
.font(.body)
.padding(5)
.foregroundColor(Color("black"))
Spacer()
}
}
You can achieve this by simply using an overlay.
Checkout the following example:
HStack {
Text("Lorem ipsum")
.font(.body)
.padding(.leading, 20)
.foregroundColor(Color.black)
Spacer()
}.overlay(RoundedRectangle(cornerRadius: 2, style: .continuous)
.frame(width: 3)
.foregroundColor(Color.gray)
.padding(.leading, 10),
alignment: .leading)

SwiftUI bug with ScrollView appearance to the scene

I have problem in SwiftUI. I created ScollView and post into some Views. (News Articles). When i launch app - ScrollView and content inside appears in dioganal way, like animation. But i need to launch app and get static ScrollView with views inside it, without any movements.
Also the same problem occurred when i open sheet view with ScrollView and Text Blocks inside. It also appers first 0.3s like dioganaly way. But how can i achive static ? Thank you all )
Place i use sheet controller (Also this block in ScrollView)
ScrollView{
ArticlePreView(image: "one", category: "Category", heading: "Header", author: "some text").onTapGesture {
self.showingDetail.toggle()
}.sheet(isPresented: $showingDetail) {
Home()
}
}
Image to understand
Video of problem
And this is place of my sheet controller
ScrollView() {
VStack {
HStack {
Text("Soem long title")
.font(.title)
.fontWeight(.bold)
Spacer()
Button(action: {
}) {
HStack {
Image(systemName: "heart.fill")
.foregroundColor(Color.gray)
}
.padding()
}
.background(
Circle()
.fill(Color.neuBackground)
)
.shadow(color: .dropShadow, radius: 15, x: 10, y: 10)
.shadow(color: .dropLight, radius: 15, x: -10, y: -10)
}
.padding(.horizontal, 35)
.padding(.top, 25)
Text("long text")
.multilineTextAlignment(.leading)
.foregroundColor(.gray)
.padding(.horizontal, 30)
.padding(.top, 20)
Text("long text")
.multilineTextAlignment(.leading)
.foregroundColor(.gray)
.padding(.horizontal, 30)
.padding(.top, 20)
Text("long text")
.multilineTextAlignment(.leading)
.foregroundColor(.gray)
.padding(.horizontal, 30)
.padding(.top, 20)
Text("long text")
.multilineTextAlignment(.leading)
.foregroundColor(.gray)
.padding(.horizontal, 30)
.padding(.top, 20)
Spacer(minLength: 50)
}
}.frame(width: UIScreen.main.bounds.size.width)
Fixed using
.frame(width: UIScreen.main.bounds.size.width)
.fixedSize()
SwiftUI is not adaptive

The leading alignment doesn't apply to the text SwiftUI

I have the following
struct hScrollView: View {
#State private var meals = ["short", "long Text", "really long text"]
var body: some View {
VStack(alignment: .leading) {
ScrollView(.vertical, showsIndicators: false) {
VStack(spacing: 30) {
ForEach(0 ..< meals.count) { count in
VStack(alignment: .leading) {
HStack(alignment: .center) { // .center is by default, so also remove
Text("\(count + 1) ")
.foregroundColor(Color.black)
.multilineTextAlignment(.leading)
.padding(.horizontal, 15)
.padding(.vertical, 5)
.background(
Capsule()
.fill(Color.black)
.opacity(0.20)
)
Spacer()
Text("\(self.meals[count]) ")
.foregroundColor(Color.black)
.multilineTextAlignment(.leading)
Spacer()
Button(action: {
print("Button was tapped")
}) {
Image(systemName: "pencil")
.foregroundColor(Color.yellow)
.font(.system(size: 25, weight: .bold))
.shadow(radius: 1)
}
}
}
.padding(.horizontal)
}
}
.frame(width: UIScreen.main.bounds.width) // set a fixed width
}
.frame(height: 500)
.frame(maxWidth: 400)
.padding(.top, 190)
}
}
}
If you noticed, the text is centered, I want it that way, but all beginning with .leading alignment so they all start at the same start point. What can I fix there? I tried putting the leading alignment in all the stacks :/
Thanks
Your view would be like
(1)|-------|short|-------|✏️
(2)|-----|long text|-----|✏️
(3)|-|really long text|-|✏️
so you should remove the Spacer() between the first Text and second instead of set text alignment.
HStack(alignment: .center) { // .center is by default, so also remove
Text("\(count + 1) ")
.foregroundColor(Color.black)
.multilineTextAlignment(.leading)
.padding(.horizontal, 15)
.padding(.vertical, 5)
.background(
Capsule()
.fill(Color.black)
.opacity(0.20)
)
// Spacer()
Text("\(self.meals[count]) ")
.foregroundColor(Color.black)
.multilineTextAlignment(.leading)
Spacer()
Button(action: {
print("Button was tapped")
}) {
Image(systemName: "pencil")
.foregroundColor(Color.yellow)
.font(.system(size: 25, weight: .bold))
.shadow(radius: 1)
}
}