UITextView content size different in iOS7
The content size property no longer works as it did on iOS 6. Using sizeToFit as others suggest may or may not work depending on a number of factors.
It didn't work for me, so I use this instead:
- (CGFloat)measureHeightOfUITextView:(UITextView *)textView
{
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
{
// This is the code for iOS 7. contentSize no longer returns the correct value, so
// we have to calculate it.
//
// This is partly borrowed from HPGrowingTextView, but I've replaced the
// magic fudge factors with the calculated values (having worked out where
// they came from)
CGRect frame = textView.bounds;
// Take account of the padding added around the text.
UIEdgeInsets textContainerInsets = textView.textContainerInset;
UIEdgeInsets contentInsets = textView.contentInset;
CGFloat leftRightPadding = textContainerInsets.left + textContainerInsets.right + textView.textContainer.lineFragmentPadding * 2 + contentInsets.left + contentInsets.right;
CGFloat topBottomPadding = textContainerInsets.top + textContainerInsets.bottom + contentInsets.top + contentInsets.bottom;
frame.size.width -= leftRightPadding;
frame.size.height -= topBottomPadding;
NSString *textToMeasure = textView.text;
if ([textToMeasure hasSuffix:@"\n"])
{
textToMeasure = [NSString stringWithFormat:@"%@-", textView.text];
}
// NSString class method: boundingRectWithSize:options:attributes:context is
// available only on ios7.0 sdk.
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary *attributes = @{ NSFontAttributeName: textView.font, NSParagraphStyleAttributeName : paragraphStyle };
CGRect size = [textToMeasure boundingRectWithSize:CGSizeMake(CGRectGetWidth(frame), MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
CGFloat measuredHeight = ceilf(CGRectGetHeight(size) + topBottomPadding);
return measuredHeight;
}
else
{
return textView.contentSize.height;
}
}
How do I size a UITextView to its content on iOS 7?
I favor this minimal code change: Just add these two lines after addSubview
and before grabbing the height
of the frame
...
[scrollView1 addSubview: myTextView];
[myTextView sizeToFit]; //added
[myTextView layoutIfNeeded]; //added
CGRect frame = myTextView.frame;
...
This is tested backwards compatible with iOS 6. NOTE that it shrink-wraps the width. If you're just interested in the height and have a fixed width, just grab the new height but set the original width, and it works just as before on both iOS 6 and 7.
(Speculation: it does size to fit on iOS 7 as well, but the layout is updated later or in a separate thread, and that this forces the layout immediately so that its frame is updated in time for using its height value a few lines later in the same thread.)
NOTES:
1) You might or might not have implemented the outer container resize this way. It does seem to be a common snippet, though, and I've used it in my projects.
2) Since sizeToFit
seems to work as expected on iOS 7, you likely don't need the premature addSubView. Whether it will still work on iOS 6 then is untested by me.
3) Speculation: The extra layoutIfNeeded
mid-thread might be costly. The alternative as I see it is to resize the outer container on the layout callback (fired or not depending on if the OS decides whether layout is needed or not) where the outer container resize will cause another layout update. Both updates might be combined with other layout updates to be more efficient. If you do have such a solution and you can show that it is more efficient, add it as answer and I'll be sure to mention it here.
iOS7 UITextView contentsize.height alternative
With this following code, you can change the height of your UITextView depending of a fixed width (it's working on iOS 7 and previous version) :
- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
{
UITextView *textView = [[UITextView alloc] init];
[textView setAttributedText:text];
CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)];
return size.height;
}
With this function, you will take a NSAttributedString and a fixed width to return the height needed.
If you want to calculate the frame from a text with a specific font you, need to use the following code :
- (CGSize)text:(NSString *)text sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
CGRect frame = [text boundingRectWithSize:size
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName:font}
context:nil];
return frame.size;
}
else
{
return [text sizeWithFont:font constrainedToSize:size];
}
}
You can add that SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO
on your prefix.pch file in your project as:
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
You can also replace the previous test SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)
by :
if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])
How do I size a UITextView to its content?
This works for both iOS 6.1 and iOS 7:
- (void)textViewDidChange:(UITextView *)textView
{
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
}
Or in Swift (Works with Swift 4.1 in iOS 11)
let fixedWidth = textView.frame.size.width
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
textView.frame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
If you want support for iOS 6.1 then you should also:
textview.scrollEnabled = NO;
IOS7 get UITextView contentSize before viewDidAppear method is called?
You can call [self.textView layoutIfNeeded]
after creating it with the appropriate frame (so it can figure out its max width) then contentSize
should be representing the content.
Also: If textView is defined as weak
, the text view will be gone on the next line (as ARC adds a release and the optimizer moves it up). They announced this optimization change for Debug (-O0) builds during WWDC.
iOS 7 - UITextView size font to fit all text into view (no scroll)
Solution 1
Your problem can be solved by simply replacing sizeWithFont: constrainedToSize:
with :
boundingRectWithSize:CGSizeMake(newView.frame.size.width, FLT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Interstate" size:fontSize]}
context:nil];
Solution 2
The sizeThatFits
method can be used to address this problem like this:
while (fontSize > minSize && [newView sizeThatFits:(CGSizeMake(newView.frame.size.width, FLT_MAX))].height >= newView.frame.size.height ) {
fontSize -= 1.0;
newView.font = [tv.font fontWithSize:fontSize];
}
I hope one of these solutions solve your problem. Cheers!
Resize UItextview according to it's content in iOS7
It seems UITextView's
contentSize
property is not correctly set in iOS 7 till viewDidAppear:
. This is probably because NSLayoutManager
lays out the text lazily and the entire text must be laid out for contentSize
to be correct. The ensureLayoutForTextContainer:
method forces layout of the provided text container after which usedRectForTextContainer:
can be used for getting the bounds. In order to get total width and height correctly, textContainerInset
property must be taken into account. The following method worked for me.
- (CGRect)contentSizeRectForTextView:(UITextView *)textView
{
[textView.layoutManager ensureLayoutForTextContainer:textView.textContainer];
CGRect textBounds = [textView.layoutManager usedRectForTextContainer:textView.textContainer];
CGFloat width = (CGFloat)ceil(textBounds.size.width + textView.textContainerInset.left + textView.textContainerInset.right);
CGFloat height = (CGFloat)ceil(textBounds.size.height + textView.textContainerInset.top + textView.textContainerInset.bottom);
return CGRectMake(0, 0, width, height);
}
Additionally, it seems UITextView's
setContentSize:
method is called from layoutSubviews
. So, calling layoutIfNeeded
on a textView
(which itself calls layoutSubviews
) after calling ensureLayoutForTextContainer:
on its layoutManager
, should make the textView's
contentSize
correct.
[someTextView.layoutManager ensureLayoutForTextContainer:someTextView.textContainer];
[someTextView layoutIfNeeded];
// someTextView.contentSize should now have correct value
UITextView contentSize changes and NSLayoutManager in iOS7
Looks like the problem is in default layoutManager of UITextView
. I've decided to subclass it and see, where and why re-layout being initiated. But simple creation of NSLayoutManager
with default settings solved the problem.
Here is code (not perfect) from my demo project (see in question). The _textView
there was an outlet, so I remove it from superview. This code is placed in - viewDidLoad
:
NSTextStorage* textStorage = [[NSTextStorage alloc] initWithString:_textView.text];
NSLayoutManager* layoutManager = [NSLayoutManager new];
[textStorage addLayoutManager:layoutManager];
_textContainer = [[NSTextContainer alloc] initWithSize:self.view.bounds.size];
[layoutManager addTextContainer:_textContainer];
[_textView removeFromSuperview]; // remove original textView
_textView = [[MyTextView alloc] initWithFrame:self.view.bounds
textContainer:_textContainer];
[self.view addSubview:_textView];
MyTextView
here is a subclass of UITextView
, see question for details.
For more info see:
- About Text Handling in iOS
- Using Text Kit to Draw and Manage Text
- NSLayoutManager Class Reference for iOS
Related Topics
How to Capture Picture with Avcapturesession in Swift
How to Add Buttons to Navigation Controller Visible After Segueing
Tableview Reloaddata VS. Beginupdates & Endupdates
How to Set a Default Value of a Uipickerview
How Does One Print All Wkwebview on and Offscreen Content Osx and iOS
Swift - Coredata Migration - Set New Attribute Value According to Old Attribute Value
Run a Swift 2.0 App Forever in Background to Update Location to Server
Xcode: Could Not Inspect the Application Package
Ios: How to Copy HTML into the Cut-Paste Buffer
Printing the View in iOS with Swift
How to Get Current Longitude and Latitude Using Cllocationmanager-Swift
Export Compliance in iOS App Submission
Uiviewcontrollerhierarchyinconsistency When Trying to Present a Modal View Controller
Ios5 Nsurlconnection Methods Deprecated
When Will Applicationwillterminate Be Called
Get Advertisement Data for Ble in iOS
Apns Notifications Not Reaching Devices Enrolled in Apple Mdm