UIWebView locating text by touch - ios

Would like to capture a touch on a UIWebView, map that to the relevant HTML text and insert a hyperlink into the HTML at that point.
Found a very nice reference to cleanly capture touches on UIWebView at:
http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way/
I've implemented it and it works very well.
Also found a very nice reference to search and highlight text in a UIWebView at:
http://www.icab.de/blog/2010/01/12/search-and-highlight-text-in-uiwebview/
I've implemented it [for a search feature] and it works very well.
But I can't quite make the intellectual jump from touch position [not text search] to a specific word in the UIWebView. Obviously this can be done, since touch-and-hold on a UIWebView triggers a "Copy or Define" popup which obviously knows the specific word you've pointed at.
The individual text nodes that JavaScript decodes from the DOM have no offsetLeft or offsetTop or offsetWidth or offsetHeight; I understand these need to be derived from the parent node.
But the individual text nodes can consist of many lines of text, depending on what the HTML source looks like. Potentially a single text node can contain an entire paragraph of text, hardly the one word that I'm trying to spot.
Can anyone suggest how "Copy or Define" can be so clever? Thanks.

Related

NSTextView - using up/down arrows moves paragraphs not lines

I have an app that runs on iOS and uses UITextView to display rich text, and also runs on Mac and uses NSTextView to display rich text. These are separate apps, but with a lot of shared code. In both cases, the {NS,UI}Textview is programmatically created and placed on a canvas.
{NS,UI}TextView will wrap long lines of text as required. Long lines are called paragraphs and have NSParagraphStyles that can be applied across the whole paragraph (line spacing, head indent etc). Each paragraph is delineated by a "\n" character (or one of the Unicode equivalents). Paragraphs wrap across multiple lines, if required.
On iOS, when you have a long paragraph covering multiple lines... if you use the down/up arrow keys, the cursor moves up and down by lines (i.e. possibly within the same paragraph), which is as you expect.
On the Mac, with the same setup, the up/down arrow keys move the cursor up and down by paragraphs (i.e. multiple lines), which is very much not what you expect.
I have attached videos of the two cursor movements so you can see what happens.
I use the same code to create the {NS,UI}TextView, and I can't see why there is a difference.
Does anyone know what's going on here? I have not interfered with the arrow key handlers in any way (although I do implement textViewDidChangeSelection: in the delegate, and can confirm that the selection is changing per my comments above).
Any ideas would be most appreciated!
Thx.
iOS Cursor Down in a UITextView.
Mac Cursor Down in an NSTextView.
Found it! It was an errant calculation in the delegate function DoCommandBySelector.

(DOORS DXL) Creating title page in Word with different size/color text

I currently have a script that creates an HTML page that shows the differences in scripts that were made since the previous baseline and then saves it as a Word document. Now, what I'd like to do is add a title page with text that is of a different color and size than the main document. This text will also need to be centered to make it look more like the other documents our company makes. Luckily, the title of the document will be the same for all versions, so I'm adding this part of the script to before it adds the differences. However, I'm not sure how to make the text display as a different color and size before adding a "new page" break for the rest of the document. Can somebody help me out with how to add the text and new page to the front of the current document?
Chris

Embedding other widgets in a GTK+ text entry

I would like to embed another widget in one end of a GTK+ 3 text entry box, a bit like many browsers do with search or protocol security "chips":
I know I can set an icon on a text entry with
entry = Gtk.Entry()
entry.set_icon_from_icon_name(Gtk.EntryIconPosition.PRIMARY, icon_name)
Even if I pass my own GdkPixbuf to set_icon_from_pixbuf this still limits me to icon sizes, when I want some arbitrary size (at least horizontally) depending on the "chip" content.
I also tried to "shunt" the text over with set_margin_left, but this moved the left edge of the whole entry box over, rather than the text within the box.
What would be an effective way to embed some other GTK+ widget (hopefully of any complexity, so I can make the chip more interactive) within a text entry?
GtkEntry is not a container, so it cannot have child widgets.
The appropriate way to implement what you see in the screenshot is to use a separate container and style it appropriately.
Have you considered putting the entry inside a frame, and restyling stuff to make it look like it was inside an entry? Then you could use standard containers to put whatever widgets next to the text entry spaces that you wanted.
The downside is that clicking your "icon" doesn't focus the entry automatically, but it makes that action totally configurable.

Add a Text Link to a TextView

Is it possible to add a text link into a TextView? I want the link to perhaps behave like a button, where I can assign an action to it.
EDIT: When I say assign an action, I mean actually giving it something in the code. I'm wondering if it's possible to dynamically add a "button" into text that I can assign a coded action to.
Live scenario
Think of something like a dictionary app. Maybe the definition of one word uses another word that you might not know the definition of, so being able to click on that word to instantly search it rather than having to type it in would be a nice user friendly feature. It seems rather unlikely, though, I guess.
I would recommend using NIAttributedLabel from Nimbus, an open source iOS library. You can specify text ranges that are links, and you get delegate messages sent when a user taps on it.
Main Nimbus site: http://nimbuskit.info/
NIAttributedLabel docs: http://docs.nimbuskit.info/interface_n_i_attributed_label.html
in the inspector, go to the Text View Attributes tab then make sure "Detect Links" is checked.
Yes you can. Add the URL into the text view, then open up the Attributes Inspector. You will see an option in there to detect links.
I know of a way, but its a LOT of work. First, you have an NSAttributedString that you have the text view display. Second, attribute the range of text you want to be the button. Third, assign a tap gesture recognizer to the text view and in the method called by the recognizer, you'll use core text to determine if the tap happened over the range of text that represents the buttons.
Heres how youll use core text: create a framesetter with the attributed string. Create a frame from the framsetter with the shape of a square that is the frame of the text view, inset by the padding of the text view. The frame will allow you to get the y origins of every line in the text view and and once you know what line the tap happened on, you can use the line to then figure out exactly what character was tapped on that line by giving it an x offset. Once you know character index on the line, you can add it to the beginning of the range of the line and get the index of the character within the whole string. Then you can check if its within the range of the text that is your button. If it is, you can then call a method to simulate a target action type behavior.
Ive explained the process of how to accomplish this and specified what kinds of core text objects youll need, ill let you look up the specific api details:
https://developer.apple.com/library/mac/documentation/Carbon/Reference/CoreText_Framework_Ref/_index.html#//apple_ref/doc/uid/TP40005304
You can also use my objc core text wrapper:
https://github.com/mysterioustrousers/MYSCoreText
What about CoreText? It Can draw many kinds of Text .

How to make a DIV get Voiceover focus when it doesn't contain any focusable HTML?

I have an email client application, and I'm changing the way we show attachments for an email. Previously we had them up at the top of the email viewer in a scrollable list; now we're moving them so that they're displayed as a list at the bottom of the email content. For reasons related to the UIWebView control, it was necessary for me to implement these attachments as HTML code that is programmatically appended to the HTML of the original email (instead of doing them like a normal person would, where each attachment is just a UIView in iOS).
I'm now trying to configure accessibility for these HTML elements and running into some problems. Each "attachment" consists of a button-like background which is actually constructed from 3 images: one for the left side of the button, one for the right side of the button, and a middle portion which is stretchable (this allows the button to stretch in width without distorting the edges). There's also a paper clip image and a right-arrow image laid out on the "button", and two sub-DIVs for displaying the file name and its size.
The problem is that I want to make the outer DIV that encompasses all of these sub-elements to be the only thing that is focusable by Voiceover, and I want to be able to control what is announced by Voiceover for this DIV (so that I can include the file name, its size, whether or not it's currently downloading etc.). However, it seems that the only things Voiceover wants to focus are the three-subimages that make up the button background (and they're announced by their filenames) and the two sub-DIVs that show the filename and the file size. If I hide all of these sub-elements from Voiceover by setting aria-hidden="true", then nothing gets focused at all. If I set aria-hidden="false" on the outer DIV, the DIV is still not focused.
Is there any way to make a DIV focusable by Voiceover even when it doesn't contain any inner HTML content that Voiceover would focus on its own? I think I could hack a solution where I stretch a completely transparent PNG over the DIV and make that the only inner element with aria-hidden="false", but even then I'm not sure how to get Voiceover to read something other than the file's name.
Update: OK, it seems I can sort of achieve this by adding role="button" on my DIV while keeping all of the sub-elements aria-hidden="true". The entire DIV is now focusable, but it still announces itself as a "button" (I'd like it to say "attachment" instead as the type, but I can live with "button" if necessary). The main problem I'm now encountering is that Voiceover seems to ignore the aria-label property, and instead reads out any text that is contained in my sub-DIVs. Is there any way I can get Voiceover to read the aria-label property that I explicity set instead?

Resources