Is there a way to create custom UIDataDetectorTypes? - iphone

What I am trying to do is create tooltip functionality so that certain words in my instructional app can be tapped and the definition pops up. For the popup part I plan on using code from “AFInformationView” which provides bubbles on the iPhone.
The part I'm struggling with is how to associate A particular word's location with the bubble. Currently I have the text on a UILabel that is on a custom UITableCell. Since I calculate the row height on the fly with:
[textToUse sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(stop-start, 500)];
I'm not sure what the coordinates for a specific word will be. I was thinking that if I created a custom DataDetectorType that could be the fix.
If anyone knows how to do this or has any other ideas I would be happy to hear them.
Thanks,
Andrew

I didn't create a custom UIDataDetectorTypes but Craig Hockenberry did something like it with his TwitterrificTouch.
He uses regular expressions to detect links and other things. I provide it with my keywords and then they become tappable. He places buttons on top of the matching text from the underlying labels. You can google a lot of posts that talk about "putting transparent buttons on top" of various things but Craig's code is the only example/working code I could find.
Here is the link:
http://furbo.org/2008/10/07/fancy-uilabels/

I don't think this is possible. The (few) Data Detector types that the iPhone currently supports are hard-coded with a integer type id. There does not seem to be a mechanism to extends that list of types.
File a feature request in their bug tracker. I will do the same.

AFAIK, you can't create custom data detectors.
The best approach for this sort of thing seems to be using UIWebViews. At least that's what I did. However, you shouldn't use a UIWebView inside a UITableViewCell. In fact, no subview of a UITableViewCell should respond to user input. So I think the best approach would be to display a UIWebView when the cell is tapped.

UIWebViews could be a possible approach but on scrolling you should consider that the whole text should be parsed to detect the words.You could use HTMl tags to make them blue and provide the links.But how could i then assign a custom behavior then opening in safari?

If you want custom data detector you could write an extractor method to primarly patch the links with help of NSregularExpression. For example
NSString regex = #"(http|https|fb)://((\w)|([0-9]*)|([-|_]))+(\.|/)"; to patch alll the links including Facebook URLs inside text like fb://friends.
Then you could use NSattributedString yo mark the links with different colors etc.
ThreeTwenty has a great library called TTTAttributedLabel where you could assign links to certain parts of a text. I also scrolls quite fast if you use it in tableviews
https://github.com/mattt/TTTAttributedLabel

Related

Monotouch.Dialog Customizing cells

I wanna customize cells in MonoTouch.Dialog.
For example make custom background in BoolElement and make custom image for this small bool thing that can be on or off, sorry forgot the name, or make custom disclosure indicator image in StringElement.
Is there simple way to do this without making own custom elements?
In some cases you'll need to create custom Element-derived types to customize cells. In other cases you will be able to add some custom code inside your application. FWIW I think it's cleaner to create your own everytime.
You can find a lot of examples in the Sample application that is available on github along with MonoTouch.Dialog (that includes having a custom background and totally owner-drawn elements).
There are also several questions (with answers) about common MonoTouch.Dialog customizations here on stackoverflow. Click on the monotouch.dialog tag and read them.
If you get stuck on a particular customization then don't hesitate to ask for help.

Adding hyperlink to any text in UITextFiled and perform a custom action - iPhone 4

I am trying to accomplish the following:
Read and Parse the text in a UITextField. Identify all the numbers in the UITextField and convert these into hyperlinks.
When these hyperlinks are clicked perform a custom action, which is to display a UIActionSheet and based on the selection assign the number (in the hyperlink) to another UITextField instance
For example if the UITextField has the text - "This is a sample test with number 123445 and more numbers 44555, 66777".
I should be able to parse the above text, detect all three numbers and add hyperlinks to them.
For the first part (parsing) I found out that there is a NSRegularExpression class that can be used to detect patterns in a text. But I could not find a way of adding hyperlinks to the matched numbers. I tried looking at Three20 documentation and could not figure out a way. Even tried the answer in this link - Just how to you use TTStyledTextLabel? but it only auto detects URLs and adds hyperlinks to them, I want to add hyperlinks to any custom text.
Can someone please help me with this. Please do not ask me to use WebView. I would really appreciate some code snippets. Thanks in advance. I am using xCode4.
If you don't need the text to be user-editable directly, you may use my OHAttributedLabel class to achieve this. (here on github)
This allows you to display any NSAttributedString and can also autodetect links, phone numbers and everything Apple's NSDataDetector class is able to detect. You can also add custom links to your label on any part of the text.
See the sample project included in my github repository for more details.
It is very customizable, both for link colors, underline style, action to perform when a link is tapped, which link types it should autodetect, and you can add any custom links and style you need on the text.

Can I use the text correction ui element on a normal UIView

I'd like to use the text correction ui element in my own UIView.
Is this allowed & possible? If so, how can I use the element?
Clarification: I don't want to use it as a text correction feature.
I want to use it as UI elements to show multiple (dismissable) tags on a UIView to show & modify predicate settings.
You can use UIMenuController, which might be close to what you want (it's the thing that shows the Cut/Copy/Paste/Replace menu in text fields). See the CopyPasteTile and Touches sample code for details.
If you actually mean the thing that pops up with spelling completion suggestions, then no, you can't easily use it. You'll have to create custom buttons as you mentioned above. But, try downloading and running UIKit Artwork Extractor to help with getting UIKit artwork to match the look of your custom buttons.
Text correction? Do you meant the auto-complete / correct features you get when you type text on iOS? If so, you get that for free as part of UITextField or UITextView, so the answer would be yes. You can define exactly what you want corrected as part of the properties of those two classes.

Rendering a Long Document on iPad

I'm implementing a document viewer with highlighting/annotation capabilities for a custom document format on iPad. The documents are kind of long (100 to 200 pages, if printed on paper) and I've had a hard time finding the right approach. Here are the requirments:
1) Basic rich-text styling: control of left/right margins. Control of font name, size, foreground/background color, and line spacing. Bold, italics, underline, etc.
2) Selection and highlighting of arbitrary text regions (not limited to paragraph boundaries, like in Safari/UIWebView).
3) Customization of the Cut/Copy/Paste popup (UIMenuController) This is one of the essential requirements of the app.
My first implementation was based on UIWebView. I just rendered the document as HTML with CSS for text styling. But I couldn't get the kind of text selection behavior I wanted (across paragraph boundaries) and the UIMenuController can't be customized from within UIWebView.
So I started working on a javascript approach, faking the device text-selection behavior using JQuery to trap touch events and dynamically modifying the DOM to change the background color of selected regions of text. I built a fake UIMenuController control as a hidden DIV, positioning it and unhiding it whenever there was an active selection region.
Not too shabby.
The main problem is that it's SLOOOOOOOW. Scrolling through the document is nice and quick, but dynamically changing the DOM is not very snappy. Plus, I couldn't figure out how to recreate the magnifier loupe, so my fake text-selection GUI doesn't look quite the same as the native implementation. Also, I haven't yet implemented the communication bridge between the javascript layer and the objective-c layer (where the rest of the app lives), but it was shaping up to be a huge hassle.
So I've been looking at CoreText, but there are precious few examples on the web. I spent a little time with this simple little demo:
http://github.com/jonasschnelli/I7CoreTextExample/
It shows how to use CoreText to draw an NSAttributedText string into a UIView. But it has its own problems: It doesn't implement text-selection behavior, and it doesn't present a UIMenuController, so I don't have any idea how to make that happen. And, more importantly, it tries to draw the entire document all at once, with significant performance degradations for long documents. My documents can have thousands of paragraphs, and less than 1% of the document is ever on screen at a time.
On the plus side, these documents already contain precise formatting information. I know the exact page-position of every line of text, so I don't need a layout engine.
Does anyone know how to implement this sort of view using CoreText? I understand that a full-fledged implementation is overkill for a question like this, but I'm looking for a good CoreText example with a few basic requirements:
1) Precise layout & formatting control (using the formatting metrics and text styles I've already calculated).
2) Arbitrary selection of text.
3) Customization of the UIMenuController.
4) Efficient recycling of resources for off-screen objects.
I'd be happy to implement my own recycling when text elements scroll off-screen, but wouldn't that require re-implementing UIScrollView?
I'm brand-new to iPhone development, and still getting used to Objective-C, but I've been working in other languages (Java, C#, flex/actionscript, etc) for more than ten years, so I feel confident in my ability to get the work done, if only I had a better feel for the iPhone SDK and the common coding patterns for stuff like this. Is it just me, or does the SDK documentation really suck?
Anyhow, thanks for your help!
Does your document have any semantic components other than each paragraph? If you already have some concept of sections or pages, I would recommend you render each one of those as an independent tablecell. It's pretty simple to create a tablecell that makes you forget you're actually looking at a UITableView. All you would need to do is override drawRect: and setSelected: and setHighlighted: and tah dah! No More cell dividers unless you want them. Furthermore you could do some nifty things by using a tableview as your base. If you defined sections in the UITableView then you could have a nifty header that scrolls along as you're paging through your document. Another thing you could do is add a "jump to section" bar / a bookmarks menu, and that way you don't have to provide selection across the boundaries of sections.
Massive copy paste blocks would be pretty painful on the system as well. Further, if you went through the trouble to provide this content you might not want to make it too easy for someone to copy it all at once... (Can't follow this line of thought more without more specifics on your project).
If you really do want to provide the copy paste options you could add buttons to each logical page or section that immediately selects and copies the whole section for the user's convenience. (Maybe with citation associated?)
I recommend you lookup the UITableViewCell UITableViewDelegate and UITableViewDataSource in the SDK docs as those pages will significantly help if you choose to use this suggestion.
Just two random observations:
Can you afford to create a paging interface? (As opposed to “endless scrolling”.) It looks like a paging interface would be a lot easier on system resources.
The UIActionBar is actually the UIMenuController class. The interface is a bit weird, as the menu is a singleton (wtf?), but I’m sure you’ll have no trouble figuring it out.
Hope that helps.
Here's a potential solution, but I don't know if it's crazy. Since I'm still so new to iPhone development, this might be a big no-no.
Anyhow, I had the idea to render each paragraph of the document (whose dimensions I've already precisely calculated) as a cell in a UITableView. Since UITableView already has mechanisms for cell recycling, I wouldn't have to implement that from scratch, and the document could be arbitrarily long without causing resource consumption problems.
Of course, I'd want to get rid of the line separators between cells, since I want the UI to look like a document instead of a table.
Or maybe I could render each page of the document (like a typical PDF, this is a paged-document format) as a table cell, and override the cell-separator graphic to look like a page boundary...
But would it be possible to get rid of the default touch behavior within the table, and instead implement text-selection on the table cell contents? Would it be completely impossible to implement text selection that crosses paragraph boundaries (between multiple table cells)?
The UIWebView is a good choise, but we need another application to pre render the pages percisely using each font and each style sheet and store the rendring information into a database table:
chapter_id int primary key,
startlocation int,
end location int,
fontsize int (or stylesheetname string)
Using JavaScript we can calculate how many words fit in a div with out scrolling.
UIWebView is good as it provide rich content and it has selection and highlighting behavior.
Hope this helps.

Best approach for adding non-web links to UITextView

I am creating a dictionary-style app that presents a tableview full of words, and when a word is selected, a UITextView is displayed that shows the definition of the word. What I would like to do is add a line that says "See also: synonym1, synonym2" where synonym1 and synonym2 are links that will take the user to the definition for the synonym that is touched.
What is the best way to add these dynamic links? Buttons? Somehow add a small UIWebView a UItable on the fly?
Thanks!
I would replace your UITextView with a UIWebView and use that contain your description text and your links. It's fairly trivial to generate HTML on the fly for something like that.
You could register a custom URL scheme for your app, or you could intercept links as they're clicked.
If your links are always grouped together there's no reason why you couldn't use a bunch of UIButtons inside a custom view, but then you'd have to handle layout and wrapping on your own. It seems a lot easier to do it in HTML.