For a given NSRange
, I'd like to find a CGRect
in a UILabel
that corresponds to the glyphs of that NSRange
. For example, I'd like to find the CGRect
that contains the word "dog" in the sentence "The quick brown fox jumps over the lazy dog."
The trick is, the UILabel
has multiple lines, and the text is really attributedText
, so it's a bit tough to find the exact position of the string.
The method that I'd like to write on my UILabel
subclass would look something like this:
- (CGRect)rectForSubstringWithRange:(NSRange)range;
Details, for those who are interested:
My goal with this is to be able to create a new UILabel with the exact appearance and position of the UILabel, that I can then animate. I've got the rest figured out, but it's this step in particular that's holding me back at the moment.
What I've done to try and solve the issue so far:
UITextView
and UITextField
, rather than UILabel
.I'd bet that the right answer to this involves one of the following:
NSLayoutManager
and textContainerForGlyphAtIndex:effectiveRange
Update: Here's a github gist with the three things I've tried so far to solve this issue: https://gist.github.com/bryanjclark/7036101
Swift5 UILabel Extension. Use this code in that case. Show activity on this post. Change the text property to attributed and select the text and right click to get the font property. Click on the strikethrough.
Following Joshua's answer in code, I came up with the following which seems to work well:
- (CGRect)boundingRectForCharacterRange:(NSRange)range { NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:[self attributedText]]; NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; [textStorage addLayoutManager:layoutManager]; NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:[self bounds].size]; textContainer.lineFragmentPadding = 0; [layoutManager addTextContainer:textContainer]; NSRange glyphRange; // Convert the range for glyphs. [layoutManager characterRangeForGlyphRange:range actualGlyphRange:&glyphRange]; return [layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer]; }
Building off of Luke Rogers's answer but written in swift:
Swift 2
extension UILabel { func boundingRectForCharacterRange(_ range: NSRange) -> CGRect? { guard let attributedText = attributedText else { return nil } let textStorage = NSTextStorage(attributedString: attributedText) let layoutManager = NSLayoutManager() textStorage.addLayoutManager(layoutManager) let textContainer = NSTextContainer(size: bounds.size) textContainer.lineFragmentPadding = 0.0 layoutManager.addTextContainer(textContainer) var glyphRange = NSRange() // Convert the range for glyphs. layoutManager.characterRangeForGlyphRange(range, actualGlyphRange: &glyphRange) return layoutManager.boundingRectForGlyphRange(glyphRange, inTextContainer: textContainer) } }
Example Usage (Swift 2)
let label = UILabel() let text = "aa bb cc" label.attributedText = NSAttributedString(string: text) let sublayer = CALayer() sublayer.borderWidth = 1 sublayer.frame = label.boundingRectForCharacterRange(NSRange(text.range(of: "bb")!, in: text)) label.layer.addSublayer(sublayer)
Swift 3/4
extension UILabel { func boundingRect(forCharacterRange range: NSRange) -> CGRect? { guard let attributedText = attributedText else { return nil } let textStorage = NSTextStorage(attributedString: attributedText) let layoutManager = NSLayoutManager() textStorage.addLayoutManager(layoutManager) let textContainer = NSTextContainer(size: bounds.size) textContainer.lineFragmentPadding = 0.0 layoutManager.addTextContainer(textContainer) var glyphRange = NSRange() // Convert the range for glyphs. layoutManager.characterRange(forGlyphRange: range, actualGlyphRange: &glyphRange) return layoutManager.boundingRect(forGlyphRange: glyphRange, in: textContainer) } }
Example Usage (Swift 3/4)
let label = UILabel() let text = "aa bb cc" label.attributedText = NSAttributedString(string: text) let sublayer = CALayer() sublayer.borderWidth = 1 sublayer.frame = label.boundingRect(forCharacterRange: NSRange(text.range(of: "bb")!, in: text)) label.layer.addSublayer(sublayer)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With