I have an app where I am bringing some formatted text from the database.
The problem is when I use:
Text(myDataComingFromJson), of course, my text will be something like:
I've created an extension where I can remove the HTML tags but then all my text will be in only one paragraph and the lists won't exist ()
The second approach would use WKWebView but then I will have some headaches formatting text or loading extra views to do the job.
Question:
Is there a way to use Text directly, but keeping break lines and lists? Even though I have to change the content at the database level ?
I also have tried using \r\n in the database but still didn't work.
Is there a better solution?
Thank you all
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I have a ForEach listing a lot of eventos, having 1 image button for each of them.
When the button is clicked, I will open a modal with the selectedEvento.
The problem is even though I've the line "self.selectedEvento = evento" inside of the button actions, when I click the button for the first time, the selectedEvento is being passed as nil.
However if I click a second button, the process will happen succesfully.
Code:
...
@State var showModal = false
@State var selectedEvento: Evento!
...
ForEach(store.eventos) { evento in
VStack() {
Button(action: {
self.selectedEvento = evento
self.showModal.toggle()
}) {
WebImage(url: evento.thumbnail)
}
}
.sheet(isPresented: $showModal) {
if self.selectedEvento != nil {
//open detailView
} else {
Text("Some Error State goes here")
}
}
}
** Why is this happening ?
Shoudn't the first click also to be passing the selectedEvento ? Why it doesnt happen ?
Thx
I have this API call in my application.
For some reason, the response is coming back nil.
If I try the right URL, a JSON will be returned and all the nodes filled out.
So I'm trying to get the error inside the
if let error = error { but even putting a breaking point in there, the code is never reached.
What am I doing wrong ?
Thanks
func getProductById(productId: Int, completion: @escaping (ProductModel) -> ()) {
guard let url = URL(string: "https://mysite/api/products/82") else { return } var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type") URLSession.shared.dataTask(with: request) { (data, request, error) in
if let data = data {
print("data") } if let error = error {
print("error") }
guard let data = data else { return } do {
let product = try! JSONDecoder().decode(ProductModel.self, from: data)
DispatchQueue.main.async {
completion(product)
}
} catch {
print(error)
} }
.resume()
I have an ObservableObject class that can return 2 different @published vars (product or products), depending on the function I call.
But when I define the published vars, I am having an error message "Missing argument for parameter 'from' in call".
It just happens for the first published var.
What's the mistake?
Thx
class ProductStore: ObservableObject {
@Published var product = ProductModel() //getting error here
@Published var products: [ProductModel] = []
func getProducts() {
ProductApi().getProducts() { (products) in
self.products.append(contentsOf: products)
}
}
func getProductById(productId: Int) {
ProductApi().getProductById(productId: productId) { (product) in
self.product = product
} }
}
struct MyView: View {
var body: some View {
//this value is coming from a JSON
var myDate = "3000-01-01T08:00:00-08:00"
//Here I'm calling my function formatDate, passing my myDate parameter:
Text(formatDate(dateString: myDate))
}
}
func formatDate(dateString: String) -> Date {
//first I dont know why my parameter gets here nil
let dateFormatter = ISO8601DateFormatter()
//at the dateFormatter, doesnt matter the options I set, the return is always the same.
dateFormatter.formatOptions = [
.withFullDate,
.withFullTime,
]
let finalDate = dateFormatter.date(from: dateString)!
return finalDate
}
I just need the get the date string, convert it to the format MM dd, yyyy - hh:mm
How can I do it ?
Thank you
I am trying to present a decimal variable containing the value of 66.67. So I am trying to do:
Text("\(myVariable, specifier: "%.2f")")
//Instance method 'appendinterpolation(_:specifier) requires that Decimal conform to _formtSpecifiable
Text("\(myVariable, specifier: "%.2f")" as String)
//I receive extra argument specifier in call
How can I fix it ?
Thx
I have set in my info.plist file, the background and image I want for my launch screen.
The image is 200px x 200px and for some reason, when the launch screen happens, the image gets stretched, not keeping the original size.
How can I fix it ?
Thank you
The main view on my app brings data from 3 different API calls.
So when the app starts, the data are still being returned, so nothing is seen but just a blank space.
They are basically a Thumbnail and its title.
What would be the best solution for that:
Have a temporary thumb and a text like "loading" OR
any other approach?
Thank you
I have a store, but need to call the method inside the body.
However the only way it works is if I call it inside init otherwise I have an error.
@ObservedObject var store = MyStore()
init(category: categoryModel) {
store.getData(categoryId: category.id) //here works
}
var body: some View {
store.getData(categoryId: category.id) //Here I will have the error: Type '()' cannot conform to 'View'
Why ? Thx
I am using SF symbols on my TabBar but even though I use the stroke version, it inserts the fill one.
ProductsListView()
.tabItem {
Image(systemName: "book")
Text("Products")
}
However, the image that shows up when I run the application in the book.fill
Why this could be happening?
Thx
I have a Loading view saying "Please wait" that I want to be presented until the data from the API comes back.
However, self.showLoading = false is executed even before store.getHomeItems() is finished.
How can I do it ?
struct HomeView: View {
@State var showLoading = false
...
if (self.showLoading == true) {
LoadingView()
} else {
//my content
}
...
ScrollView {
//my scroll content
}
.onAppear {
self.showLoading = true
DispatchQueue.main.async {
store.getHomeItems()
}
self.showloading = false
...
}
I also dont know if I can call Dispatch on my HomeView, since my API layer has also an another DispatchQueue.
URLSession.shared.dataTask(with: url) { (data, _, _) in
guard let data = data else { return }
let myItems = try! JSONDecoder().decode([ItemModel].self, from: data)
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
completion(myItems)
}
}
.resume()
Thank you
I have a picker on my view but it's not selectable unless I replace my VStack for a NavigationView.
However I cant have a NavigationView on this page, otherwise it brakes other parts.
What's the solution for that ?
Thx
VStack {
Picker
}
I have a button that when pressed should call an API which return an ID.
Then having this ID, I will call a 2nd API.
Button(action: {
var id = self.callApi1()
self.callApi2(id)
}) {
Text("Start")
}
How can I call the Api2 JUST when the Api1 was finished and returned its value ?
Thank you
In my SwiftUI project, I am trying to convert HTML to Text.
Text("h1Test/h1p Test/pbr/spanTest/spanulliitem1/liliitem2/li/ul")
.font(.body)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.bottom, 16)
Is there a way to make it happen ?
Thank you
In my SwiftUI app, I have a TabBar with 2 items:
a) TabItem1
b) TabItem2
However on the View itself, I created 2 buttons:
a) Button A
b) Button B
Now I need to switch TabBar items, using the buttons on the View. How can I do this ?
*******
Button(action: {
//call tabItem 1
})
{
Text("TabItem1")
}
Button(action: {
//call tabItem 2
})
{
Text("TabItem2")
}
***************************
TabView {
TabView1View()
.tabItem {
Text("TabItem1")
}
TabView2View()
.tabItem {
Text("TabItem2")
}
Thank you