开发者

Getting text based on height

开发者 https://www.devze.com 2023-04-05 11:04 出处:网络
We all know that we can calculate the height of a label or any control according to the text. Like this:

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 c开发者_运维问答omes 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.


As per your need you can change the label text depend on your interest. Here is my sample code.

NSMutableString *tmpLabel2=[[NSMutableString alloc]init];
NSString *text=@"Hello friend what r u doin..? what is going on in your company.. Tell me something yar i want to meet with u whenever u free just call me i will be der ok rest is perfect. talk u later…";     
NSMutableString *tmpLabel1 = [[NSMutableString alloc] initWithString:text];
NSRange range = NSMakeRange([tmpLabel1 length] - 1, 1);

CGSize  labelsize=[text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
while ([tmpLabel1 sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(100, 2000.0)].height > 200) {

    unichar Char=[tmpLabel1 characterAtIndex:[tmpLabel1 length]-1];
    NSString*strTemp=[NSString stringWithFormat:@"%C",Char];
    [tmpLabel2 insertString:strTemp atIndex:0];
    [tmpLabel1 deleteCharactersInRange:range];
    range.location--;
}

label.frame=CGRectMake(50, 50, labelsize.width, 200);
label.text=tmpLabel1;
label.font=[UIFont fontWithName:@"Arial" size:14];
label.numberOfLines=0;
label.clipsToBounds=YES;
label.adjustsFontSizeToFitWidth=YES;
label.lineBreakMode=UILineBreakModeCharacterWrap;
label.backgroundColor=[UIColor grayColor];

NSLog(@"first Label is: %@", tmpLabel1);
NSLog(@"Second Label is: %@", tmpLabel2);

}

0

精彩评论

暂无评论...
验证码 换一张
取 消