2

I want to get the coordinates of a substring of attributed text. This is a simplified version of the premise.

// Content
NSString *haystack = @"This is a paragraph of moderate length. I want to find a specific word within it. And then draw on top of that Word a rectangular shape.";
NSString *needle = @"word";

// Finding the needle
NSRange range = [haystack rangeOfString:needle];
range.location; // 66
range.length;   // 4

// Draw it
UILabel *label;
label.text = haystack;

The range only gives me a character-wise location of the needle within the haystack. I need a bounding CGRect of the needle. Here's what I want to achieve, having this UILabel.

Before

Draw a rounded rect over the needle.

After

Morgan Wilde
  • 16,795
  • 10
  • 53
  • 99

0 Answers0