iOS 5 : bug with loadHTMLString with style - ios

I have these lines of code:
NSString *html = [NSString stringWithFormat:
#"<html><body><p \"style=\"font-size:%d; text-align:center; color:red; \
direction:rtl;\">%#</p></body</html>", fontSize, thePage];
mailBody.text = thePage;
[self.page loadHTMLString:html baseURL:nil];
Styling didn't take any effect at all. So, direction isn't set, color is black instead of red and the font-size isn't set to the number I put in the variable. Yet, it's working in pre iOS 5.

There is a problem in your HTML
USE THIS
NSString* html = [NSString stringWithFormat:#"<html><body><p style=\"font-size:%d;text-align:center; color:red; direction:rtl;\">%#</p></body></html>",fontSize,thePage];

Related

NSAttributedString justified text (without stretching words)

I'm using justified text for my NSAttributedString in my UITextView:
var paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Justified;
normalAttributes.setObject(paragraphStyle, forKey: NSParagraphStyleAttributeName)
The problem is that it stretches some words in order to achieve the justified alignment, instead of just stretching the spaces between the words. I find this very distracting and know there is a way to do such that words are not stretched, but only the spacing. Below is a screenshot from my app, followed by one from an app with some of the same functionality that also uses justified text and the exact same font.
How do I achieve the same effect?
Any help would be greatly appreciated.
There is a nasty trick which you can use.
You can use a UIWebView and css to do the alignment the way you want.
Here is a sample:
NSString *css = [NSString stringWithFormat:
#"<html><head><style>body { background-color: white; text-align: %#; font-size: %ipx; color: black;} a { color: #172983; } </style></head><body>",
#"justify",
11];
NSMutableString *desc = [NSMutableString stringWithFormat:#"%#%#%#",
css,
#"15 Προσέχετε ἀπὸ τῶν ψευδοπροφητῶν͵ οἵτινες ἔρχονται πρὸς ὑμᾶς ἐν ἐνδύμασι προβάτων͵ ἔσωθεν δέ εἰσι λύκοι ἅρπαγες. 16 ἀπὸ τῶν καρπῶν αὐτῶν",
#"</body></html>"];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(20, 80, 150, 100)];
[self.view addSubview:webView];
[webView loadHTMLString:desc baseURL:nil];
And here is the result:
You just have to change the hyphenationFactor to 1:
textView.layoutManager.hyphenationFactor = 1

UIWebView content loading at wrong position

I am trying to add three UIWebView to my iOS app. I have successfully read the "HTML code" into the UIWebView and it displays the content fine, but one of the three views are displayed at a wrong position.
Here is the code I used to load the data:
-(void) webViewLoading {
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"load" ofType:#"html"];
NSString* embedHTML = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[self.choice_1_webview loadHTMLString: embedHTML baseURL: nil];
[self.choice_2_webview loadHTMLString: embedHTML baseURL: nil];
[self.choice_3_webview loadHTMLString: embedHTML baseURL: nil];
}
Here is what I got in the simulator:
and here is what I have in Xcode:
The first UIWebView does not load correctly and the grey rectangle is the background color of the UIWebView. Does anyone know what could have caused this? I created the three UIWebViews exactly the same (copied and pasted in the interface builder).
===
It looks like the content of the first UIWebView has a smaller height than the other two. It is weird however because the contents are generated from the same file.

Select parts of a UIWebView

I want to open my website in a UIWebView but I need not to shown the upper and lower part of it. So I want for example to select the following part: top + 50 pixels --> bottom - 50 pixels
Is it possible to do in iOS?
just a demo:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *js = [NSString stringWithFormat:
#"var html = document.body.innerHTML;"
#"var newHtml = \"<div style='overflow:hidden;'><div style='margin-top:-50px; margin-bottom:-50px; display:block;'>\" + html + \"</div></div>\";"
#"document.body.innerHTML = newHtml;"
];
[webView stringByEvaluatingJavaScriptFromString:js];
}

Set font size content UIWebview

I built an application to read mail on the ipad, when displayed html body content to UIWebView the font size it is not uniform, I can not adjust the font size as all the other applications MailBox, 'Gmail'..
I use:
html {
-webkit-text-size-adjust: none; /* Never autoresize text */
}
but does not solve the problem, how to fix it?
try this one it worked for me
- (void)webViewDidFinishLoad:(UIWebView *)wView {
[wView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '50%'"];
[wView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('body')[0].style.zoom= '0.5'"];
}
You can set font size of webview , like
NSString *fontsize;
fontsize=20px;
NSString *WebviewString;
WebviewString=#"xyz";
Put the value of fontsize and WebviewString in this:
[MywebView loadHTMLString:[NSString stringWithFormat:#"<div style='text-align:justify; font-size:%#;font-family:Helvetica;color:#ffff;'>%#",fontsize,WebviewString] baseURL:nil];
Hope it will helpyou.

Set dynamic height of webview depending on the content on an iPhone

I want set dynamic height for webview depending on the dynamic text. I've tried
[my_webview sizeThatFits:CGSizeZero];
But it didn't work for me. I want to set webview height dynamically.
See my answer to a similar question. The trick is to set the frame of the webView to a minimal height prior to sending -sizeThatFits:.
Set up your web view as follows:
- (void)webViewSettings {
NSString *htmlString = #"Your HTML String";
NSString* descHtmlString = [NSString stringWithFormat: #"<html><meta name=\"viewport\" content=\"width=300, user-scalable=yes\" /><div style='width:300px' id='ContentDiv'><font face=\"Arial\" size=2.5 color='#702f70'>%#</font></div></html>", htmlString];
descHtmlString = [descHtmlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[self.webView loadHTMLString:descHtmlString baseURL:[NSURL URLWithString:#""]];
}
Then implemented UIWebViewDelegate in your interface file and the following method in the implementation file:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *contentHeight = [self.descriptionWeb stringByEvaluatingJavaScriptFromString:#"document.getElementById('ContentDiv').offsetHeight"];
self.descriptionWeb.frame = CGRectMake(10,webView.frame.origin.y, 300, [contentHeight floatValue]);
}

Resources