Reputation: 2444
I am trying to show a HTML data in WKWeView, But it's showing half content only. I tried to give more space to webView but its still cutting last paragraph.
I am unable to scroll below this-
My Code is:
func get_detail_feeds(){
FeedsMangerClass.get_feed_details(feed_Id: feedID!) { (json,error) in
if error == nil{
self.list.removeAll()
self.final.removeAll()
let script = "<meta name="+"viewport"+" content="+"initial-scale=1.0"+">"
for (i,txt) in json["fullText"].enumerated(){
let str = txt.1.stringValue + script
self.list.insert(str, at: i)
self.final += str
}
let font = UIFont.init(name: "Avenir-Medium", size: 15)
self.feedKitView.loadHTMLString("<span style=\"font-family: \(font!.fontName); font-size: \((self.fontSize)); color: #000000\">\(self.final)</span>", baseURL: nil)
}
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.feedKitView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
self.feedKitView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
print(webView.scrollView.contentSize.height)
print((height as! CGFloat))
self.scrollView.contentSize.height = 0.0
self.feedKitView.frame.size.height = 0.0
self.scrollView.contentSize.height = ((height as! CGFloat) + (self.screenSize.height))
self.feedKitView.frame.size.height = self.scrollView.contentSize.height
}
})
}
})
}
My HTML Paragraph -
"fullText" :[
"<p>The Biden administration proposes substantial tax increases on U.S. corporations to pay for the $2.3 trillion plan in order to boost investment in infrastructure, clean energy, manufacturing, housing, and other programs.<\/p><p>Amid concerns that a higher corporate tax rate would damage the country’s market competitiveness, Yellen announced on April 5 that the United States is working with the Group of 20 (G-20) countries to adopt a minimum global corporate income tax.<\/p><p> <\/p><blockquote class=\"twitter-tweet\">\n <p lang=\"“en”\" dir=\"“ltr”\"><\/p>\n <p><a href=\"“https:\/\/twitter.com\/insidevoa\/status\/1380830416035328000”\"> April 10, 2020<\/a><\/p>\n<\/blockquote><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><p><\/p><p>Biden’s eight-year infrastructure plan is to be fully paid for by tax increases on companies spread over 15 years. His proposal is to boost the federal corporate tax rate to 28 percent from the current 21 percent.<\/p>",
"<div class=\"visible-xs visible-sm\"><h4 style=\"padding: 0 0 20px; font-size: 18px\"><a href=\"\/blab\/pop\/269182target=\"_blank\">NYT: EMAILS SHOW TRUMP TRANSITION OFFICIAL SAYING RUSSIA 'HAS JUST THROWN THE USA ELECTION TO HIM'<\/a><\/h4><\/div>",
"<p>Yellen noted if an agreement among G-20 economies is reached on the minimum corporate tax, it would end “a 30-year race to the bottom.”<\/p><p>“Together, we can use a global minimum tax to make sure the global economy thrives based on a more level playing field in the taxation of multinational corporations, and spurs innovation, growth, and prosperity,” she declared.<\/p><p> <\/p><blockquote class=\"twitter-tweet\">\n <p lang=\"“en”\" dir=\"“ltr”\"><\/p>\n <p><a href=\"“https:\/\/twitter.com\/ShiplakeEcon\/status\/1380835749344477187”\"> April 10, 2020<\/a><\/p>\n<\/blockquote><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><p><\/p><p>Both France and Germany, which have relatively high corporate tax rates in the OECD welcomed the Biden administration’s proposal.<\/p><p>“We are delighted by the U.S. support for a minimum corporate tax,” French Finance Minister Bruno Le Maire told AFP.<\/p>",
"<div class=\"visible-xs visible-sm\"><h4 style=\"padding: 0 0 20px; font-size: 18px\"><a href=\"\/blab\/pop\/508382target=\"_blank\">FEDS REPORT MILLENNIALS FACE $1 TRILLION IN DEBT, MAJORITY IS STUDENT LOANS<\/a><\/h4><\/div>",
"<p>“An agreement on international taxation is now within reach,” he noted. “We must seize this historic opportunity.”<\/p><p>The average statutory corporate tax rate among Organization for Economic Co-operation and Development (OECD) countries had fallen to 23.3 percent in 2020 from 32.2 percent in 2000, according to a report by the Treasury Department.<\/p><p> <\/p><blockquote class=\"twitter-tweet\">\n <p lang=\"“en”\" dir=\"“ltr”\"><\/p>\n <p><a href=\"“https:\/\/twitter.com\/inspatpty\/status\/1380846011413897219”\"> April 10, 2020<\/a><\/p>\n<\/blockquote><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><p><\/p><p>The report, which outlines Biden’s “Made in America tax plan” stated that the declines have resulted from a “collective action problem.” Fierce competition among countries to attract investments has fueled this “race to the bottom,” according to the report, and a better coordination across the globe will prevent companies from relocating to countries with lower taxes.<\/p>",
"<div class=\"visible-xs visible-sm\"><h4 style=\"padding: 0 0 20px; font-size: 18px\"><a href=\"\/blab\/pop\/390478target=\"_blank\">DONALD TRUMP INSULTS JOHN MCCAIN'S MILITARY CAREER IN NEWLY UNEARTHED VIDEO FROM 1999<\/a><\/h4><\/div>",
"<p>During this week’s virtual spring meeting of the International Monetary Fund (IMF) and the World Bank, IMF Managing Director Kristalina Georgieva also called for “progressive taxation” and an agreement on “minimum taxation for companies.”<\/p><p>The IMF has long supported a global minimum tax, according to IMF’s top economist Gita Gopinath.<\/p><p> <\/p><blockquote class=\"twitter-tweet\">\n <p lang=\"“en”\" dir=\"“ltr”\"><\/p>\n <p><a href=\"“https:\/\/twitter.com\/ManuelRuizAdame\/status\/1380847060451217409”\"> April 10, 2020<\/a><\/p>\n<\/blockquote><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><p><\/p><p>“It is a big concern that we have a large amount of tax shifting, tax avoidance, countries sending money to tax havens, and that’s reducing the tax base from which governments can collect revenues and do the necessary social and economic spending that’s required,” Gopinath told reporters. “So we are very much in favor of a global minimum corporate tax.”<\/p>",
"<div class=\"visible-xs visible-sm\"><h4 style=\"padding: 0 0 20px; font-size: 18px\"><a href=\"\/blab\/pop\/366982target=\"_blank\">TRUMP: NORTH KOREA 'TOTAL DENUCLEARIZATION' STARTED; OFFICIALS SEE NO NEW MOVES<\/a><\/h4><\/div>",
"<p>OECD officials earlier floated a minimum corporate rate of 12.5 percent. Yellen, however, proposed agreeing to a minimum tax rate of 21 percent.<\/p><p>World Bank President David Malpass warned governments against setting a minimum tax rate that is too high.<\/p>"
]
Upvotes: 1
Views: 404
Reputation: 329
I have tried with your sample data and I am able to scroll to the bottom if I commented feedKitView
and scrollView
height. Also, I tried this in a UITabBarController.
import WebKit
class TabBarItem1: UIViewController, WKNavigationDelegate {
var viewDelegate: ViewControllerDelegate? = nil
@IBOutlet var feedKitView: WKWebView!
@IBOutlet var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
feedKitView.navigationDelegate = self
getDetailFeeds()
}
func getDetailFeeds() {
guard let path = Bundle.main.path(forResource: "feeds", ofType: "json") else {
fatalError("Sample data not available")
}
var final = ""
var list = [String]()
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path))
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: [String]] {
// try to read out a dictionary
print(json)
final.removeAll()
let script = "<meta name="+"viewport"+" content="+"initial-scale=1.0"+">"
if let fullTex = json["fullText"] {
for (i,txt) in fullTex.enumerated(){
let str = txt.debugDescription + script
list.insert(str, at: i)
final += str
}
}
self.feedKitView.loadHTMLString("<span style=\"font-family: Avenir-Medium; font-size: \((15)); color: #000000\">\(final)</span>", baseURL: nil)
}
} catch {
return
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.feedKitView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
self.feedKitView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
print(webView.scrollView.contentSize.height)
print((height as! CGFloat))
// self.scrollView.contentSize.height = 0.0
// self.feedKitView.frame.size.height = 0.0
// self.scrollView.contentSize.height = ((height as! CGFloat) + (self.view.frame.height))
// self.feedKitView.frame.size.height = self.scrollView.contentSize.height
}
})
}
})
}
}
Upvotes: 1
Reputation: 6178
Maybe you can try KVO:
self.webView.scrollView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
DispatchQueue.main.asyncAfter(deadline: .now()+0.1) {
webHeight = self.webView.scrollView.contentSize.height
}
}
Upvotes: 0