I have this interface from IB. As you can see in 'Details' UITextfield
there are a lot of HTML code inside it.
I have a PHP server behind so that I can make it in the back-end too. What is the best way to perform this?
I have this interface from IB. As you can see in 'Details' UITextfield
there are a lot of HTML code inside it.
I have a PHP server behind so that I can make it in the back-end too. What is the best way to perform this?
You can convert your HTML to string using this :
NSRange range;
NSString *string;
while ((range = [string rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound){
string=[string stringByReplacingCharactersInRange:range withString:@""];
}
NSLog(@"Un block string : %@",string);
But if your text contains some <> it will be removed
you should use a parser to parse html, even if it is just a small fragment. AKV proposes to use RegEx, but it is highly doubtful that a correct RegEx can be found, see also here. i.e. <a href="foo" title="5>3">
is valid html that would confuse AKV's Regex.
iOS has a C-lib onboard that can be used, libXML2, but actually you should use some wrapper around it, I think Objective-C-HMTL-Parser is simple and great.
you would use it like
HTMLParser *parser = [[HTMLParser alloc] initWithString:html error:&error];
HTMLNode *bodyNode = [parser body];
NSString *string = [bodyNode allContents];
As HTMLNodes can contain other HTMLNodes as children you can use that information for textformatting too.
There is also the great DTCoreText project for parsing HTML to NSAttributedStrings, if this is an option
Display it in a UIWebView with a transparent background, that way you can have all of your HTML tags still functioning. Just make the UIWebView the same size as your UITextView and put it in the same spot!
When you are loading your HTML string into your webView use this code for a transparent background.
finalhtmlstring = [NSString stringWithFormat:@"<html><head></head><body style='background-color:transparent;'>%@</body></html>",htmlstring];
[myWebView loadHTMLString:finalhtmlstring baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
And also make sure you (in the xib file) set the webView's background itself to have 0% alpha and toggle the "opaque" box...
Lastly don't forget to set the delegate in your xib file and add the <UIWebViewDelegate>
to your .h file.