How make NSTextView not selectable, but allow to select links - swift

So I have NSTextView with attributed string which contain links from html markup. I want to make links clickable, but other text don't.
Delegate method which apple propose
textView:willChangeSelectionFromCharacterRanges:toCharacterRanges:
(in topic https://developer.apple.com/documentation/appkit/nstextview/1449297-selectable)
is not working as I want, I can only remove selection after it already has been selected
And property isSelectable block any mouse events, so link are not clickable either

Related

How to add a page break to uitextview

How do you add a page break to a uitextview, just like with google docs, Microsoft word, and pdf viewers? I am making an app in which there will be a text editor whose appearance is similar to that of these word processors, so I need to be able to include page breaks in the editable UITextView (I need editable blocks within the uitextview basically). How do I achieve this with SwiftUI or UIKit & Interface Builder?
I believe the short answer is "you don't." A UITextField is not a text editor, and I don't think it supports the concept of pages.

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.

showing hyperlink in iphone

I have a field in sqlite db which has multiple links inside it separted by comma. I displayed one link by placing it in a label and placing an invisible button over it and capturing the click of the button.Is there any other way to show these hyper links as the number of links in the field may change.
Use UITextView which automatically detects phone numbers, http links, and so on, if its dataDetectorTypes property is set properly to a required UIDataDetectorTypes value.

Large scrollview table with buttons

We are trying to write a training manual application for the iPhone. On the top half of the screen is a diagram of a car engine, on the bottom half is some text. At the user repeatedly hits a "next" button, we highlight different parts of the engine, and in concert we highlight different parts of the descriptive text below.
We basically want "living text" in the text half, with the illustration following along on top to where the reader is in the text. What we'd like from the text is 1. user can scroll it using their thumb so possibly a UIScrollView 2. the software can explicitly drive a scroll to any part of the text (when they hit the "next" button). 3. the words in the text are interspersed with hotlinks e.g. "this is the camshaft... this is the piston..." and the user should be able to click on any of the keywords like camshaft, piston, and have the diagram highlight that. (The problem is not highlighting the diagram, its capturing the click). The text would have 300~400 buttons/links/keywords and about 600 words of text.
Since this is fairly similar to using a web browser, we tried using Apple's version of webkit using a UIWebView and handleOpenURL to register a service back to the app itself. But Webkit for internal links a popup comes up asking permission to access that link. Every single the user wants to go to a link (in our case just an internal event that we'd intercept so that we can highlight e.g. the camshaft). Tried to intercept the event from the HTML view, but that didn't work.
It seems like the best we can do is to abandon scrolling text, and make the text part more like flash cards or a power point presentation, breaking the text into custom UIViewCells with buttons inside a UIScrollView. However, this would impose an annoying constraint on the author that they would have to write everything to fit in the UIViewCells, sort of chunky.
Any ideas would be appreciated.
This is definitely something you can use a UIWebView for. Don't use handleOpenURL, rather, set your viewController as the webview's delegate, and override -webView:shouldStartLoadWithRequest:navigationType:. When this gets called, check the request, and pull out your link data from there.
It would probably be easier to implement that completely in JavaScript in the document you load in a UIWebView. You would have to use JavaScript (i.e. [UIWebView stringbyevaluatingjavascriptfromstring:]) anyway to achieve things like scrolling to a certain position.

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.