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!
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.
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