iOS: A lot of Interactive text for ebook - ios

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.

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.

How to create custom keyboard extension with images on it in Objective-C?

I am having an app in which I want to create my own custom keyboard.
In this custom keyboard, I want to put images.
Users can access this keyboard from anywhere in the device.
I want it like this Link.
I want to make a keyboard like this
I have searched a lot on this and I know there are lots of tutorials but most of those are in swift and another are not with keyboard extension.
I want proper guidance or any link of tutorial for this.
Any help would be highly appreciated.
Thanks...
So I got my answer from the below link which is a very nice tutorial in objective C.
Custom keyboard with extensions in objective c for ios8
Now, like I needed, if anyone wants to add custom images in it, you can just copy the image to the clipboard and paste the image where its needed.
For copying the png files, use the below code.
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString *imageName = [NSString stringWithFormat:#"%ld",(long)sender.tag];
NSString *newPathName = [[NSBundle mainBundle]pathForResource:imageName ofType:#"png"];
NSData *data = [NSData dataWithContentsOfFile:newPathName];
[pasteboard setData:data forPasteboardType:#"public.png"];
This public.png comes from http://www.escape.gr/manuals/qdrop/UTI.html
You can choose any extension type you want to.
The pasteboard type changes as per the image extensions.
Important note: For image sending, the Apps which gives an access to paste the image copied from clipboard, only from those apps images will be sent.
I did it with the above solutions. Hope it helps someone else also.
Thanks...
This question is several years old now, but if anyone has the same needs today, I have created a Swift library that helps you create keyboard extensions that support characters, operations like backspace, newline etc. as well as images. It also has support for copying images to the pasteboard and saving them to the photo album.
If you need a library like this, feel free to check it our here:
https://github.com/danielsaidi/KeyboardKit

How to highlight multiple text fragments on iOS?

The past few days, I have been searching for a suitable solution to highlight multiple text fragments on iOS. The general idea is similar to how text highlighting works in Amazon's Kindle application.
I have experimented with UITextView as well as UIWebView, but I haven't find the right solution in terms of usability and performance.
The idea is simple. The user taps a sentence and the sentence is highlighted. When another sentence is tapped, that sentence is highlighted as well. The built-in solution for highlighting text is not suitable for this purpose. An important aspect of the solution is that the text needs to be styled, which is possible with UITextView since iOS 6.
My question is fairly general, that is, what are some viable approaches to implement this type of functionality? Are there any open source solutions that do what I describe?
I will suggest you to NSAttributedString on multiline UILabels. Starting from iOS 6 UILabels by default supports attributed strings without any modifications.
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"Some text and some more %#", dynamicString]];
//Change background color to highlight a sentence, by giving sentence's range
[str addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(sentenceStartIndex, sentenceLength)];
[label setAttributedText:str];
EDIT: It turns out UITextViews also support NSAttributedString I didn't try that, so you may replace UILabels with UITextViews.
I recommend using TTTAttributedLabel from Matt Thompson. It supports attributed strings, works all the way back to iOS 4, and is MIT licensed.

Adding a lot of dynamic formatted text to iOS App

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 !

Want to copy a gif image from my app to the in-app mail window. How do i do that?

I am having some confusion here...
how do i display gif for my iPhone app?
(I mean there are various articles which i found, but every tutorial just didn't have a complete solution)
There are many solutions between which i am confused.
Should i split gif into different frames using an online tool and then display it in UIImageView using animation?
Or should i display gif into a UIWebview?
Or should i use this article?
http://blog.stijnspijker.nl/2009/07/animated-and-transparent-gifs-for-iphone-made-easy/
This article is pretty nice but it cuts the rest of the frames making the image smaller in size. Moreover, i am not able to know how its working so i dont know the reason of frames getting cut.
Please note that after i display the gif, i want to copy the gif and then paste it in my in-app mail window.
Need help plz. Thanks!
UIWebView is a the biggest memory-hog in UIKit and should be avoided whenever possible. Converting gif to series of png files and displaying it using UIImageView with animated image sequence is best you can do.
But to use it in the mail window, you would have to keep the gif too. Now how you want to insert it depends on if you want it to be an attachment or a html img. I would suggest the second option, and hosting the gif somewhere online, then doing:
[mailController setMessageBody:#"<img src=\"http://path.to/image.gif\" />" isHTML:YES];
And if you want to add it as attachment:
NSData *imageData = [[NSData alloc] initWithContentsOfFile:pathToGifFile];
[mailController addAttachmentData:imageData mimeType:#"image/gif" fileName:#"pic.gif"];
[imageData release];

Resources