I have a UITextView
that has a maximum number of four lines. I want to put UILabels
behind each line, covering only however much text there is for the respective line.
So do any of you fine coders have any ideas as to how I would calculate each line's width?
try this one
UIFont *font=[UIFont systemFontOfSize:17.0f];
UILabel *yourLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 80)];
yourLabel.numberOfLines=4;
yourLabel.text=@"Hello\nHulo\nMe\nyou";
CGSize labelSize = [yourLabel.text sizeWithFont:font
constrainedToSize:yourLabel.frame.size
lineBreakMode:NSLineBreakByTruncatingTail];
CGFloat singleLineHeight = labelSize.height/yourLabel.numberOfLines;
[self.view addSubview:yourLabel];
A single line height will be about 21.
NSLog(@"single line height is %f",singleLineHeight);
For text View follow this code
UITextView *yourView=[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
yourView.font=[UIFont systemFontOfSize:14.0f];
yourView.text = @"Hello \nmy\n Friend\n Hru? ";
CGSize textViewSize = [yourView.text sizeWithFont:yourView.font
constrainedToSize:CGSizeMake(yourView.frame.size.width, FLT_MAX)
lineBreakMode:NSLineBreakByTruncatingTail];
[yourView setFrame:CGRectMake(0, 0, textViewSize.width, textViewSize.height)];
[self.view addSubview:yourView];
NSLog(@"single line height is %f",yourView.frame.size.height);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.