I have a UITextView
inside a UIScrollView
that worked perfectly fine on iOS 6
built from xcode 4.x
, however now building with xcode 5
it doesn't work properly, even on iOS 6
.
The problem is the text wraps with the screen width even though the UITextView
and UIScrollView
have large widths. I use this code to work out the new width and height of the UITextView
, and even though the textview scrolls left/right the text is wrapped as if the width is only the width of the screen.
Thanks
self.responceTextView.text = [NSString stringWithFormat:@"%@%@",_responceTextView.text,responce];
[self textViewDidChange:self.responceTextView];
- (void)textViewDidChange:(UITextView *)textView
{
// Recalculate size of text field
CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
CGSize reqSize = [textView.text sizeWithFont:[UIFont fontWithName:@"Courier" size:12] constrainedToSize:maxSize lineBreakMode:NSLineBreakByClipping];
self.responceTextView.frame = CGRectMake(0, 0, reqSize.width+16, reqSize.height+16);
// Resize scroll view if needs to be smaller so text stays at top of screen
CGFloat maxScrollHeight = maxScrollViewSize.size.height;
if (self.responceTextView.frame.size.height < maxScrollHeight) {
self.responceScrollView.frame = CGRectMake(self.responceScrollView.frame.origin.x, self.responceScrollView.frame.origin.y, self.responceScrollView.frame.size.width, self.responceTextView.frame.size.height);
} else {
self.responceScrollView.frame = maxScrollViewSize;
}
// Set content size
self.responceScrollView.contentSize = CGSizeMake(self.responceTextView.frame.size.width, self.responceTextView.frame.size.height);
[self scrollToCursor];
}
EDIT ----
Ok, so it seems sizeWithFont
is deprecated in iOS 7. Strange how I get no compiler warning.
It still doesn't make sense that it doesn't work on iOS 6 (or is it completely removed when built with iOS 7 SDK?)
I have tried these 2 alternatives, but get exactly the same size back from all.
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Courier" size:12], NSFontAttributeName,
nil];
CGRect rect = [textView.text boundingRectWithSize:maxSize options:NSLineBreakByClipping | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil];
returns: {{0, 0}, {439.27148, 168}}
CGSize rect2 = [textView.text sizeWithAttributes:attributes];
returns: {439.27148, 168}
And the original above returns {439.27148, 168}
They should all return a wider view.
EDIT 2 ---- It seems from above that the returned frame is correct (439 wide) however it's the text that is still being word wrapped inside the textview.