Get frame of word in a substring in a multiline label

Iulian Onofrei

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
|            |
|            |
|____________|
Iulian Onofrei

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get a substring after a certain word

From Dev

Get very next word of a substring in a string

From Dev

Javascript Regex - Get first word after substring

From Dev

Get frame of the title Label inside the UIButton swift

From Dev

PHP: capture a multiline substring

From Dev

iOS7 - Adjusting font size of multiline label to fit its frame

From Dev

C# Get line of multiline String starting with specific word

From Dev

C# Get line of multiline String starting with specific word

From Dev

Get size of SWT Label with word wrap

From Dev

Get size of SWT Label with word wrap

From Java

Multiline label in UIStackView

From Dev

multiline TextField - floating label

From Dev

Multiline Label On UIButton Swift

From Dev

CCLabelTTF multiline label not working

From Dev

How to use substring function to get last part of a word in SAS?

From Dev

How to use substring function to get last part of a word in SAS?

From Dev

Get the text after specific word for substring c#

From Dev

Dynamically adding multiline label in a view

From Dev

How to make multiline label with Eureka

From Dev

QML Label max width & multiline

From Dev

Dynamically adding multiline label in a view

From Dev

How to get the size (height) of a label after the word wrap

From Dev

Substring in Data Frame R

From Dev

Get the first word in a String of words & spaces - Substring first word before space

From Dev

Read string word by word with .Substring?

From Dev

highlighting subString in word

From Dev

Substring word in C#

From Dev

Multiline UILabel without word wrap?

From Dev

Can't substring multiline content in an element