7

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.

Harry Ng
  • 1,070
  • 8
  • 20

1 Answers1

2

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
ScottyBlades
  • 12,189
  • 5
  • 77
  • 85