Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting a word and shows tooltip in iOS

I would like to implement the Medium iOS App like effect for tapping highlight and shows tooltip.


Medium Highlight Tooltip


I have been researching on Text Kit and some other stackoverflow questions have some thoughts on it, please also suggest what's the better alternative to this.

Scenario:

  1. Static Text pre-defined
  2. Shows highlights in several words or phrases

Solution thoughts:

  1. Use UITextView for storing text
  2. Use attributed string for text content
  3. Showing background color using NSBackgroundColorAttributedName
  4. Detect the selection by layoutManager.characterIndexForPoint(...)
  5. Shows tooltip next to the selection
  6. Shows tooltip using one of these pods AMPopTip, CMPopTipView, EasyTipView

Right now, I am not able to select the word and shows the tooltip just next to it. Any tier of help is appreciated.

like image 901
Harry Ng Avatar asked Aug 16 '15 08:08

Harry Ng


People also ask

How to add a tooltip to a SwiftUI view?

After you added the package, all you need to do is import it and you can add a tooltip to any SwiftUI View in that file! As the first example, the Text view is provided as the tooltip content and it's attached to the other Text view. Below you can see the example of code that is required to create the tooltip and the result you see on the screen.

How to quickly select text on iPhone and iPad?

Lastly, if you want to select the entire paragraph, tap any word within the paragraph four times in quick succession. Once it’s highlighted, you’ll have access to cut, copy and paste tools located right above the selected portion. And that’s how the gestures for quick selecting texts on your iPhone and iPad work.

How do I select the trailing period in iOS text?

Triple-tap a word to select the sentence containing that word. This includes the trailing period. Quadruple-tap does the same as a double, only it selects the entire paragraph. One of the most annoying text-selection tasks in iOS is trying to copy a URL, a phone number or an email address. If those strings are on a webpage, good luck.

What's wrong with text selection in iOS 13?

There are a few text-selection oddities in iOS 13. The most annoying is that, when you tap in the middle of a word, the text-selection cursor appears at either the end or the beginning of the word. On the Mac, if you click the cursor between the letters M and A of “Mac,” that’s where it goes.


1 Answers

enter image description here

Here is a way to Highlight text.

Create an IBOutlet named myLabel

In ViewDidLoad type

let yourString = "This is how you highlight text"
myLabel.text = yourString

let malleableString = NSMutableAttributedString(string: yourString)

let numberOfCharactersToHighlight = 5
let startingIndex = 1
malleableString.addAttribute(NSBackgroundColorAttributeName, value: .magenta, range: NSRange(location: startingIndex, length: numberOfCharactersToHighlight))

myLabel.attributedText = malleableString
like image 183
ScottyBlades Avatar answered Oct 24 '22 13:10

ScottyBlades