Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting text based on height

We all know that we can calculate the height of a label or any control according to the text. Like this:

    NSString *text=@"fwfgwefgwefhwefhwoefhwoeifhoiwefhwoeifhwieofhweohfiweofowefhowefhoweifhweofhweofhweoihfweiofhiowefhweiofhwioefhweiofhiweofhweiofhweiofhweiofhweiofweoifiweofhweoifhiowefhoiwefhowewoefoiwehfoiwe";     
    labelsize=[text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 2000.0)];
    NSLog(@"labelsize.height%f",labelsize.height);

Now suppose I get height=270. Now I want only that text which lies in 200 height. Like My label height is 200 and I want that till 200 height text comes in label and rest of the text should show in another label. So I want to ask if it is possible to get the text based on height.

Thanks in advance!

like image 735
Gypsa Avatar asked Oct 09 '22 23:10

Gypsa


1 Answers

CGFloat maxHeight = 500;
NSString *text = @"fwfgwefgwefhwefhwoefhwoeifhoiwefhwoeifhwieofhweohfiweofowefhowefhoweifhweofhweofhweoihfweiofhiowefhweiofhwioefhweiofhiweofhweiofhweiofhweiofhweiofweoifiweofhweoifhiowefhoiwefhowewoefoiwehfoiwe";
NSMutableString *tmpText = [[NSMutableString alloc] initWithString:text];
NSRange range = NSMakeRange([tmpText length] - 1, 1);
while ([text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 2000.0)].height > maxHeight) {
    [tmpText deleteCharactersInRange:range];
    range.location--;
}
NSLog(@"result: %@", tmpText);
[tmpText release];

I think this can do the job. It is not fully tested, but it works.

like image 151
Infinite Possibilities Avatar answered Oct 12 '22 22:10

Infinite Possibilities