How do I locate the CGRect for a substring of text in a UILabel?
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];
}
UILabel get CGRect for substring of text
This was mostly needed for one character rects, so I went crazy and wrote some code that could accurately calculate the rect for the most basic UILabel. Standard UILineBreakMode and text alignment.
I was hoping that if I release it to the public people code contribute to it and improve it, especially since I don't know so much about text rendering!
The code:
https://gist.github.com/1278483
Find coordinates of a substring in UILabel
Might be you can calculate using-
CGSize stringSize = [string sizeWithFont:myFont
constrainedToSize:maximumSize
lineBreakMode:self.myLabel.lineBreakMode];
Now New position-
x = self.myLabel.frame.origin.x + stringSize.width
and for y similarly you need to have code with consideration of x.
UILabel centered text CGRect?
I ended up using UILabel.textRect. It works perfectly for me.
let myRect:CGRect = _topBanner.textRect(forBounds: _topBanner.bounds, limitedToNumberOfLines: 1)
_icon.frame.origin.x = myRect.origin.x - _icon.frame.size.width + 50
_icon.frame.origin.y = _topBanner.frame.origin.y + 8
Get frame of word in a substring in a multiline label
I solved it using this:
+ (CGRect)getFrameOfString:(NSString *)substring inLabel:(UILabel *)label {
NSString *string = label.text;
CGRect labelFrame = label.frame;
NSRange substringRange = [string rangeOfString:substring];
NSString *prefixString = [string substringToIndex:substringRange.location];
NSStringDrawingContext *stringDrawingContext = [[NSStringDrawingContext alloc] init];
stringDrawingContext.minimumScaleFactor = label.minimumScaleFactor;
CGRect prefixFrame = [prefixString boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, labelFrame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:[Util getAttributesforFont:label.font] context:stringDrawingContext];
CGRect substringFrame = [substring boundingRectWithSize:labelFrame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:[Util getAttributesforFont:label.font] context:stringDrawingContext];
prefixFrame = CGRectIntegral(prefixFrame);
substringFrame = CGRectIntegral(substringFrame);
NSInteger labelWidth = labelFrame.size.width;
BOOL isOnNextLine = prefixFrame.size.width >= labelWidth;
CGFloat left = prefixFrame.size.width - (isOnNextLine ? labelWidth : 0); // the substrings on the other lines are offset by the label frame's width
CGFloat top = isOnNextLine ? substringFrame.size.height : 0; // the substrings on the other lines have the top position equal to it's height
CGFloat width = substringFrame.size.width;
CGFloat height = substringFrame.size.height;
return CGRectMake(left, top, width, height);
}
which I think it now only works for labels with two lines.
Related Topics
Why Nsuserdefaults Failed to Save Nsmutabledictionary in Ios
Swift Saving and Retrieving Custom Object from Userdefaults
Uicollectionview - Dynamic Cell Height
How to Validate an E-Mail Address in Swift
Issue Detecting Button Cellforrowat
Add Image to Uialertaction in Uialertcontroller
How to Build For Armv6 and Armv7 Architectures With iOS 5
How to Determine Uiwebview Height Based on Content, Within a Variable Height Uitableview
How to Enable_Bitcode in Xcode 7
How to Load Storyboard Programmatically from Class
How to Play Audio in Background With Swift
How to Calculate the Size of a Folder
Detect If the App Was Launched/Opened from a Push Notification
Uibutton: Making the Hit Area Larger Than the Default Hit Area
Add Placeholder Text Inside Uitextview in Swift
How to Determine the Os Version At Runtime in Os X or iOS (Without Using Gestalt)