I am making an iPad app that uses UIWebView to display PDFs. I have a PDF that I would like to programmatically add links to. For simplicity, lets say there are 10 paragraphs. They are all numbered and have a few lines of text in them. I want to be able to somehow add a basic link to the PDF so that if paragraph 2 is touched, then my UIWebView can process the request that is associated with paragraph 2.
I have no idea what the structure of the PDF is like on the inside. I have no clue how to scale this to each paragraph of several hundred pages. But I am wondering if I can somehow add a link or HTML to the PDF so that I can manipulate it with my app.
Thanks!
To be clear, I am viewing this PDF on an iOS device but I recognize that the solution to my question might not have anything to do with Cocoa-touch frameworks. I am looking for any sort of solution that will allow me to add invisible links to certain areas of my PDF.
If you want an iPad app to recognize text fields, buttons, links from a pdf. You can edit the actual pdf (you'll need a version of Adobe Acrobat) and add those fields to the pdf. In your ios code parse the pdf fields using something like:
in a parse method:
-(void)parse:(CGPDFPageRef)page
{
for (int i = 0; i < CGPDFArrayGetCount (annotations); i++)
CGPDFArrayGetCount returns the number of items in a PDF array.
in the loop grab the field name:
if (CGPDFDictionaryGetString(dict, "T", &stringRef))
{
char *s = (char *) CGPDFStringGetBytePtr(stringRef);
fieldName = [NSString stringWithCString:s encoding:NSUTF8StringEncoding];
}
check to see what you want to do if a field name matches, say "button_1" or "hlink_3":
if ([fieldName isEqualToString:#"hlink_3"])
{
// do whatever, example add a button where the field was
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = rect;
[self addSubview:button];
}
There's a lot more to it, but this is the general idea.
Related
I have Objective C code that continually updates a set of numerical values. I need to display these values on the screen. That's it! I can convert numerical values into a text string, no problem. But how do I display this string in a UI element? Do I use a text box or a text field or a text view, or a...? I cannot find examples to show how to pass a string from code into the UI. I assume I need to set up a text thingy, and then periodically refresh the contents of that text thingy when the values change?
I assume the answer is simple but is just obscured by a smokescreen of technical jargon.
Thanks!
From the UI perspective you might want something like a
UITextView - multi-line text input
UITextField - single-line text input
UILabel - just text
For your purpose of just printing text, you should use UILabel, since you dont want / need any kind of input. You can access its text using:
// yourLabel is your current UILabel* you want to output yourValue to
yourLabel.text = yourValue;
Of course that yourValue needs to be converted to NSString before.
To actually get hold of the UILabel, you need to connect it from the Interface Builder as an IBOutlet. For tutorials on that topic, take a look at tutorials like Interface Tutorials by Ray Wenderlich or youtube or just google Interface Builder tutorial.
I have been struggling with this. User needs to enter text and/or emoticons in a TextView. I got an emoticon keyboard with my own images to enter emoticons. Problem is I need to keep a symbol (e.g. "(smile)" for the emoticon within the text while AT THE SAME TIME showing the emoticon picture on top of the symbol.
So user would see "Hello [the picture]" while the TextView.text property would return "Hello (smile)".
On Android you can use Spanned strings which allow you to cover part of your text with an image. Thus on Android I managed to achieve my objective without problem.
On iOS, I thought Attributed Strings were a similar concept to Spanned but so far all I have been able to do is entirely replace the emoticon's code with the picture (using NSTextAttachment). Is there a way to achieve my objective without having to maintain one attributed string containing pictures and one separate string containing codes?
You can use this method, Hope it will work for you.
- (NSAttributedString*) parseEmoticons:(NSAttributedString*)text {
text = [text stringByReplacingOccurrencesOfString:#":-)" withString:#"😄"];
text = [text stringByReplacingOccurrencesOfString:#";P" withString:#"😜"];
text = [text stringByReplacingOccurrencesOfString:#"B-)" withString:#"😎"];
text = [text stringByReplacingOccurrencesOfString:#";-P" withString:#"😜"];
return text;
}
Having failed to find a more elegant solution, I resorted to maintaining one attributedstring containing the emoticon picture, and one regular string to hold the emoticon codes. So my attString is for instance "Hello [Smiling picture]" while my string is "Hello %101%". If you are interested in building a chatting app as I am, here is the pseudo code:
In emoticon keyboard:
{
Insert picture into attributed string at location loc;
Call textView shouldChangeTextInRange:(loc,0) replacementText:"%101";
}
In the view controller at shouldChangeTextInRange:(loc,length) replacementText:text:
{
Parse regular string to jump over emoticon codes already there to find the location matching loc;
Replace text (for instance %101%) in regular string} at the identified location.
}
Note: shouldChangeTextInRange is also called for regular keyboard entries including delete.
I'm currently implementing a translation application. One that when a user selects a word from a UITextView the application translates to a different language.
I would like to restrict the options for selection to only whole words for example:
"Hello World" and not "Hello Wo"
Similar to the iOS Kindle application when defining words in the dictionary.
I have the UITextViewDelegate in my .h file.
I currently have in my .m:
- (void)textViewDidChangeSelection:(UITextView *)textView{
[self translate];
}
and
- (void)translate {
if(![[self.descriptionTextView selectedTextRange] isEmpty]) {
NSString * selectedWord = [self.descriptionTextView textInRange:[self.descriptionTextView selectedTextRange]];
...
I then go on to convert this to an encodedString and fires off the Google Translate API. Is there a way to only select whole words from the UITextView? So that the selection increments in whole word chunks.
You can add yourself as the inputDelegate of the text field and implement selectionDidChange:. When the selection changes you can use positionWithinRange:atCharacterOffset: to investigate around the selected range for white space. Once you've found your desired range you can update the selectedTextRange.
Is it possible to embed StageText within a MovieClip?
I've added about 20 instances of StageText to a page (each page is a MovieClip), and I was hoping to have all these text fields move, fade, etc. along with the MovieClip.
Right now when I fade the MovieClip out, the StageText remains and I have to remove them individually by script.
Here's an example of the code I'm using to add the StageText
var textField1:StageText = new StageText();
textField1.softKeyboardType = SoftKeyboardType.PUNCTUATION;
textField1.returnKeyLabel = ReturnKeyLabel.GO;
textField1.stage = this.stage;
textField1.viewPort = new Rectangle(800, 200, 100, 30);
textField1.text = String(0);
I'm thinking it would have something to do with the stage = this.stage; line or perhaps the viewPort, but there isn't much information on Google about StageText so I can't find anything on that.
No. I think they are just overlayed overtop of your flash content:
http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/flash/text/StageText.html
"When native inputs are used, StageText objects are not display objects and you cannot add them to the Flash display list. "
I want to build an app for iPad like a book, with two pages, with one column each.
After read some text from a database, I want to fulfill the two pages (with the right quantity of text), and save whats left to the next page. I will display some images between the text, and I need to make the text and image to fit in each page.
I'm thinking in use a UIWebView to hold the text.
My problem it's calculate the amount of text needed for each page and the text that left.
Can you help me doing that?
I will display some images between the text, and I need to make the text and image to fit in each page.
UIWebView is the wrong UI element to use if you just want to display text. You should know that loading a UIWebView takes long and eats up a lot of memory. This is a job for UITextView.
To calculate the text that will fit on a page, you will have to use the methods that UIKit adds to NSString. Here is the documentation. These help you to calculate the size a string will be when rendered with a certain font and certain line break mode.
CGSize renderedTextSize = [myString sizeWithFont:myFont forWidth:myTextView.frame.size.width lineBreakMode:UILineBreakModeWordWrap];
You could write a method that would use these methods calculate a substring of the text that will fit on your current "page" (UITextView). Then, for the next "page", start from where you left off on the last page and add text to that string until it fits perfectly in your text view (maybe you add words until it gets too big to fit, then take out the last word and return that substring).
Something like that should work.