I want to find the position and size of a word from a label's text that has 2 lines.
Let's assume that this is my label:
____________
| |
|This is the |
| |
|label |
|____________|
and I want to find the frame of the word label
, which would be something like {{0, 24}, {40, 24}}
but by using
- (CGRect)getFrameOfString:(NSString *)substring fromString:(NSString *)string inLabel:(UILabel *)label {
NSRange range = [string rangeOfString:substring];
NSString *prefix = [string substringToIndex:range.location];
NSStringDrawingContext *stringDrawingContext = [[NSStringDrawingContext alloc] init];
stringDrawingContext.minimumScaleFactor = 0.5;
CGRect prefixFrame = [prefix boundingRectWithSize:label.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:[Util getAttributesforFont:label.font] context:stringDrawingContext];
CGRect substringFrame = [substring boundingRectWithSize:label.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:[Util getAttributesforFont:label.font] context:stringDrawingContext];
prefixFrame = CGRectIntegral(prefixFrame);
substringFrame = CGRectIntegral(substringFrame);
return CGRectMake(prefixFrame.size.width, prefixFrame.origin.y, substringFrame.size.width, substringFrame.size.height);
}
I get {{100, 0}, {40, 24}}
as if the word was positioned like:
____________
| |
|This is the |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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments