I am trying to parse some html inside my app.
Not an entire html page, but just few blocks of html code.
So I'm using Ink package for that.
// FIRST I HAVE A WEBVIEW
import SwiftUI
import WebKitstruct WebView : UIViewRepresentable {
var html: String func makeUIView(context: Context) -> WKWebView {
return WKWebView()
} func updateUIView(_ webView: WKWebView, context: Context) {
webView.loadHTMLString(html, baseURL: nil)
}}
// IN HERE I'M PARSING THE HTML AND ALSO ADDING SOME STYLING TO IT
import Foundation
import Ink
class ParseContent: ObservableObject {
var markdown: String = ""
func parse() -> String {
let parser = MarkdownParser()
let html = parser.html(from: markdown)
let htmlStart = "<HTML><HEAD></HEAD><BODY style=\"padding: 140px; font-size: 120px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Open Sans,Helvetica Neue,sans-serif\">"
let htmlEnd = "</BODY></HTML>"
return htmlStart + html + htmlEnd
}
}
// IN HERE I AM JUST USING MY PREVIOUSLY CREATED WEBVIEW
WebView(html: myHtmlContent)
.onAppear() {
htmlContent = ParseContent().parse()
}
So here are my questions:
Why the styling isn't working since I am concatenating the html ? I see it in a lot of tutorials but cant understand why not working.
I would like to have the font inside my WebView, the same as I have for my other components like Text("abc") so I would keep a pattern and the visitor woudnt realize they are different components.
How to set the WebView without a vertical scroll ? Keeping the entire height coming from the parsed html ?
Thank you guys :)
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Created
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
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
When I run my app with my phone connected, suddenly the app crashes.
I don't have any information on the console, but I have the following message error in the @main line.
Thread 1: EXC_BAD_ACCESS (code=1, address=0x28)
@main
struct MyApp: App {
How can I know what is the problem and where to check for message errors when the app crashes ?
Thank you
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
I have a button in MyViewA and after clicking on the button search, I want to call MyViewB, passing the variable search.
How can I call a different view after clicking a button ?
var search: String = "abc"
Button(action: {
MyViewB(keyword: self.search)
}) {
Text("Search")
}
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
My app works fine but depending on something (that I still don't know), the app suddenly crashes.
The only message error I have is:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x5c)
How can I debug it and know exactly why this is happening ?
Thank you,
My app suddenly crashes exposing this message error:
EXC_BAD_ACCESS (code=1, address=0x5c)
I tried to debug but can't find a solution.
I believe using Instruments could be easier to find it. However Instruments has a lot of items. What would be the one responsible for debugging this kind of error I'm having ?
Thank you
My app crashes in specific parts and I am trying to identify why.
I am using Instruments - Leaks but when I access this problematic part of the app, the app crashes and nothing is shown.
What would be the best tool under Instruments umbrella to check these crashes?
Thanks
I have an array with 10 different categories.
So I create NavigationLink for each of them, calling MyListView and passing data about the category.
NavigationView {
List(categories) { category in
NavigationLink(destination:
MyListView(category: category)
)
{
CategoryRow(category: category)
}
}
}
So when I click in one of these 10 categories, I will call MyListView. Inside MyListView I have a call to the API bringing a lot of information about the chosen category.
The problem here is, even before clicking in any category, MyListView is being called and the API is called for all the 10 categories.
I only want to call MyListView and the API inside it, after selecting the category.
Whats the problem here ?
Thx
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
When I use TabView and select a TabItem, a chosen View will open right away.
TabView {
HomeView()
.tabItem {
Image(systemName: "house")
Text("Home")
}
}
Now I am trying to do the same, using button.
However with button, the chosen View will open under .sheet or .fullScreenCover.
Button(text) {
self.isPresented.toggle()
}
.sheet(isPresented: $isPresented) {
self.content
}
How can I open the View using button, the same way it's opened using TabBar ?
Thx
I have a TabBar with 5 different views for each TabBar item.
All these 5 views have long content wrapped in a ScrollView.
So let's say I chose my 2nd TabBar item and scroll on this view.
Then I move away to another view and when I hit the 2nd TabBar item again, I want this view scrolled to the top and not at the position I left on the first visualization.
How can I do that?
Thx