Several replies on the web suggest using UITextView to show (simple) HTML text. I also found some code that show how to use it in XCode:
NSString *htmlString = @"
Header Subheader Some text
";
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
textView.attributedText = attributedString;
(from https://gist.github.com/romaonthego/6672863)
However, after several hours of tweaking I still have problems doing this with Firemonkey and the iCL components. Below is my unfinished attempt to show html text in the component TMSUITextView . It runs, but still shows the raw text without parsing the html tags. If everything else is correct, I am only missing setting the lNSDictionary to the correct values, but how?
uses
iOSapi.Foundation, Macapi.Helpers, FMX.TMSNativeUICore;
var
lString: String;
lNSData: NSData;
lNSDictionary: NSDictionary;
lAttrString: FMX.TMSNativeUICore.NSAttributedString;
begin
lString := '<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p>';
lNSData := StrToNSStr(lString).dataUsingEncoding(NSUnicodeStringEncoding);
lAttrString := FMX.TMSNativeUICore.TNSAttributedString.Create;
lNSDictionary := TNSDictionary.Create;
//How to set lNSDictionary to NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
lAttrString.initWithData(lNSData,lNSDictionary,nil,nil);
TMSUITextView.TextView.setAttributedText(lAttrString);
Pieter
(Pieter)
January 5, 2015, 5:07am
2
Hi,
You can use the following code:
var
lString: String;
lNSData: NSData;
lNSDictionary: NSDictionary;
lAttrString: FMX.TMSNativeUICore.NSAttributedString;
begin
lString := '<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p>';
lNSData := NSStrEx(lString).dataUsingEncoding(NSUnicodeStringEncoding);
lNSDictionary := TNSDictionary.Wrap(TNSDictionary.OCClass.dictionaryWithObject((CocoaNSStringConst(libUIKit, 'NSHTMLTextDocumentType') as ILocalObject).GetObjectID,
(CocoaNSStringConst(libUIKit, 'NSDocumentTypeDocumentAttribute') as ILocalObject).GetObjectID));
lAttrString := FMX.TMSNativeUICore.TNSAttributedString.Wrap(FMX.TMSNativeUICore.TNSAttributedString.Wrap(FMX.TMSNativeUICore.TNSAttributedString.
OCClass.alloc).initWithData(lNSData, lNSDictionary, nil, nil));
TMSFMXNativeUITextView1.TextView.setAttributedText(lAttrString);
end;
Kind Regards,
Pieter
It works! Thank you so much.