简体   繁体   中英

UITextView breaks line automatically if string is shorter than 2 lines

I set up a UITextView that acts strange. If I set it's content to a long text it breaks it correctly at the boundaries however if the content is single lined it automatically breaks it before the last word even if it fits in one line. It created sort of a minimum of lines.

I wasn't able to find this issue posted on the web so I had to ask.

Here's the set up of the UITextView

Message * displayedMessage = [messages objectAtIndex:indexPath.row];
UIFont * messageFont = [UIFont fontWithName:@"Helvetica" size:13];
CGSize messageSize = [displayedMessage.content sizeWithFont:messageFont constrainedToSize:CGSizeMake(240.f, 3000.f) lineBreakMode:UILineBreakModeWordWrap];
UITextView * messageTextView = [[UITextView alloc] initWithFrame:CGRectMake(46.f, 0.f, messageSize.width, messageSize.height + 25)];

messageTextView.backgroundColor = [UIColor clearColor];
messageTextView.font = messageFont;
messageTextView.editable = NO;
messageTextView.scrollEnabled = NO;
messageTextView.contentInset = UIEdgeInsetsZero;    
messageTextView.textColor = [UIColor blackColor];
[messageTextView setDataDetectorTypes:UIDataDetectorTypeAll];
messageTextView.text = displayedMessage.content;
[cell addSubview:messageTextView];

I managed to solve it eventually i just had to define the textview like this, with a fixed width:

> UITextView * messageTextView = [[UITextView alloc]
> initWithFrame:CGRectMake(46.0f, 0.0f, 240.0f, messageSize.height +
> 25)];

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM