Adding a lot of dynamic formatted text to iOS App - ios

I have a big issue with my current development stage on a project that I'm working on.
I'm have a UIScrollView which holds 50 multiline UILabels with dynamic content loaded from a localizable.string. The labels are individually formatted (font, bold, italic, color).
The problem is that the App's real memory usage jumps to almost 70MB (live bytes 3MB) and that is just unacceptable and with my current concept I would have to use ~200 UILabels in order to achieve my goal.
What can I do ? Is there a way to lazy load the UILabels or reuse them ? Should I use UITextView or UIWebView ?
How can I do that ?
Thanks.

I would recommend using something like https://github.com/AliSoftware/OHAttributedLabel or a UIWebView OHAttributedLabel should have a much smaller memory foot print than a WebView But it depends how many web views you would actually need.
if you use a UIWebView
You'd draw a web view in your view, if you are using IB create and hook up outlets and then in your .m load the html formatted text into via loadHTMLString:baseURL:

Well guys with your help, I've came up with this solution which works just extraordinary !
I'm using a html "template" file with markers inside it and a UIWebView.
Basically what I'm doing is the following:
get the html file path.
create a string with the contents of html template.
replace the markers from the html with my strings (NSLocalized strings - lots of text).
load into the WebView contents of the newly created string using "loadHTMLString".
Result: from a memory footprint of 70MB, now I have a memory footprint of 12MB (and that with the equivalent of ~20 A4 pages of text).
Here's the code:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"yourhtmlfile" ofType:#"html"];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[_webView setBackgroundColor:[UIColor clearColor]];
[_webView setOpaque:NO];
NSString *htmlBody = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF16StringEncoding error:nil];
htmlBody = [htmlBody stringByReplacingOccurrencesOfString:#"//marker_1//" withString:NSLocalizedString(#"localizedKey_1", nil)];
htmlBody = [htmlBody stringByReplacingOccurrencesOfString:#"//marker_2//" withString:NSLocalizedString(#"localizedKey_2", nil)];
htmlBody = [htmlBody stringByReplacingOccurrencesOfString:#"//marker_3//" withString:NSLocalizedString(#"localizedKey_3", nil)];
[self.webView loadHTMLString:htmlBody baseURL:baseURL];
And now I can use the html goodies like text formatting :D !
I hope that the above will help a lot of people :) !
Thank you guys for support !

Related

Formatting strings in UIWebView

I'm building an iOS app which supports iOS 9 and above.
I'm using a UIWebView to display text stored in RTF documents which I am including in my app's bundle. I use the following code to insert the contents of the RTF files into the web view:
NSURL *filePath = [[NSBundle mainBundle] URLForResource:self.detailItem withExtension:#"rtf"];
NSURLRequest *request = [NSURLRequest requestWithURL:filePath];
[self.detailWebView loadRequest:request];
I am also using this to format the text to the correct size:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *fontSize = #"80";
NSString *jsString = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", [fontSize intValue]];
[self.detailWebView stringByEvaluatingJavaScriptFromString:jsString];
}
I never usually use JavaScript, so I'm not claiming to know how that works, but it enables me to adjust the fontSize string and achieve exactly the size that I want. The text displays in the same font as the rest of the stings in my app and everything looks fine.
The problem is that I want to be able to display some other text in the web view but I want to load it from an NSString in code, so I can append other strings and manipulate what the user sees. When I do this instead of loading from the RTF file, I get very different formatting:
[self.detailWebView loadHTMLString:#"This is a string" baseURL:nil];
This comes out in some nasty Times New Roman style font, and the text way smaller than the text that is loaded from the RTF files. I realise I can build some html tags into my string to add formatting, but I want to understand why the js formatting in webViewDidFinishLoad is not being applied, and what I can do to achieve universal formatting across all the strings I use in my web view.
If you need some UIWebView text formatting on iOS, you can look at my approach with SWIFT. https://github.com/Vanyaslav/Swiftyyjsformatter
Hope, it helps.

iOS: A lot of Interactive text for ebook

We're making an app and going to write and include a lot of text. I want to make the text as appealing as possible, like adding movies, images, nice headers, etc...
I have about 100 A4 pages of text, and will be adding more, so I must find a good way to organise it, and make it easy to modify.
How would you go about doing this?
I thought about these methods:
1. Put each chapter in a txt file and add tags (e.g ) for different elements, and then parse the text in the app with TextKit to layout the elements.
2. Make a PDF for each chapter and display it.
None of these seems very good. I looked at iBook author which seems nice, but I can't seem to find any possibilities to export and include the material straight into the app?
Is there maybe some other software that lets you build the book, and then export it in some nice way so you can parse it in iOS?
I recently was facing the same issue and we decided to go with RTF files. They are highly customizable and you allow the user to be able to copy the text which is nice. Plus text is always rendered clearly and RTF files keep an application small. Using PDF's can make an app unnecessary large.
They are also very easy to read:
NSString *fileName = #"impressum.rtf";
NSURL *url = [[NSBundle mainBundle] URLForResource:[fileName stringByDeletingPathExtension] withExtension:[fileName pathExtension]];
NSError *error;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithFileURL:url
options:#{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType}
documentAttributes:nil
error:&error];
CGRect paragraphRect = [attributedString boundingRectWithSize:CGSizeMake(_textLabel.frame.size.width, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
context:nil];
UpdateFrameHeight(_textLabel, paragraphRect.size.height);
[_textLabel setAttributedText:attributedString];
The only thing we didn't do is implement images right into the RTF's. We inserted spaces in the text files where the images would go an then added them as a subview to the textview.

Custom Font not always displays correct in webView

I am using a costum font in my eBookReader-App for a book requires it to display formulars.
The font is added to the app and most of the times it is displayed correct.
But sometimes it isn't and the formulars are display large and fat.
I have no idea why this happens. The fonts are in copied into the bundle and are available:
The css in the book looks like this:
The css is linked in each xhtml-file the webView is loading:
Why is it working most of the times but sometimes not?
You need set the url on the webview (then your app can find images and fonts from the project):
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[myWebView loadHTMLString:myHtmlString baseURL:baseURL];

How to keep UIWebView from caching images?

I am using UIWebView to display an HTML page inside an IOS app. The HTML page contains a png file that I create on the local disk and write to. Everything works fine the first time I display the page, but when I try to create a new image and redisplay the page, the original image is displayed.
The issue appears to be that I am using the same file name for the PNG file. Even though I write to the PNG file with a new image, the UIWebView is caching the image from the original load, and displays the original image, not the new one. I have verified that the new image is being written to correctly by loading it into Safari.
So how can I clear the UIWebView's cache of this image? I realize that another option would be to give the png file a different file name each time I create it, but then I'd either accumulate png files or I'd have to add code to clear out the png files when done - which I'd rather not do.
Have a look at this answer:
NSString *testURL = [NSString stringWithFormat:#"%#?t=%#", url, randQuery];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:testURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]];
You can try:
NSString *theURL = [NSString stringWithFormat:#"%#?t=%#", url, [[NSProcessInfo processInfo] globallyUniqueString]];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:theURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:8.0]];

integrating PDF reader

How can I create a pdf reader in iphone? It should open within the same framework of my application. I have got code for creating PDF files but nothing for viewing a PDF. can anyone help me on dis ground?
You can use UIWebView or Quartz to display PDFs.
For UIWebView use this:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake( 0, 0, 320, 480 )];
NSString *path = [[NSBundle mainBundle] pathForResource:#"something" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
webView.scalesPageToFit = YES;
[self.view addSubview:webView];
[webView release];
I'm currently building an app that uses a PDF document as will. There are 2 way's to display a pdf document, by far the simplest (but slowest) way is using a UIWebView.
The other option is building some sort of reader yourself by using the quartz2d libraries.
Apple has some good documentation on it: quart2d programming guide.
And an Example called zooming PDF Viewer.
Avoid WebViews, they are slow and you have no control over them.
The best way to render PDFs on iOS is using the CGPDF* set of functions available with Quartz.
Be aware that it won't be super easy. Over time I have found and/or contributed to numerous questions on SO about PDFs on iPhone. Check those out:
Fast and Lean PDF Viewer for iPhone / iPad / iOs - tips and hints?
CGPDF iPhone/iPad memory problems
Get PDF hyperlinks on iOS with Quartz
I have created a PDF renderer here from apple's code. Just swipe left or right to browse through pages, pinch to zoom etc.
Here is my github.
Github PDF test

Resources