UITextView can't change size with auto layout - ios

I have a XIB file containing a view with a UITextView as subview. This textView is 30px high, because it should resize according to it's content.
So I set the text programmatically, call -sizeToFit and reconfigure the frame of the textView and its superview:
[_textView setText:[contents objectForKey:#"body"]];
[_textView sizeToFit];
CGSize txtSize = _textView.contentSize;
txtSize.height = _textView.frame.size.height;
_textView.contentSite = txtSize;
CGRect superFrame = _textView.superView.frame;
superFrame += _textView.contentSize.height;
_textView.superView.frame = superFrame;
The superView actually does resize correctly. Also, when logging the text views frame and content size, on the console it appears to be okay (about 1200px high).
But the textView is still just 30px high on the screen. Also calling -setNeedsLayout did not do the trick.
What could be the issue here?

This is a bug i ran into a couple of days ago. It seems that UITextView will cut off a part of the text no matter the height if its scroll is disabled.
A solution that works in certain situations is:
[_textView setScrollEnabled:YES];
[_textView setText:text];
[_textView setScrollEnabled:NO];
I have reported this bug to Apple, but I suggest you do the same.

Related

iOS UItextview inside UIPageViewController not scrolling

I'm using UIPageViewController in my app that has 3 pages. On third page, there is a UITextView but that doesn't scroll when it has text bigger than its frame size. What could be wrong here?
set size of textview by its contentSize property
CGRect myFrame = self.myTextView.frame;
myFrame.size.height = self.myTextView..contentSize.height;
self.myTextView..frame = myFrame;
Or u can also try
[myTextView sizeToFit];

Scroll the text in UITextview when texts are less and height of UITextview is large

I have one UITextView whose height is 568 (means fit in screen size) and if i write 50 lines in it then it will definitely scroll vertically.
But i want to scroll (or just bounce) when there is only one line of text.
When user scroll Vertically then the text will just bounce in UITextView.
in my application the UITextView is not Editable.
Any idea, code,link will be great help...
EDIT:
The UITextView's height will be that 568 only.
it will not change (means without changing the height of UITextView, set this thing).
[txtView setContentOffset:CGPointMake(0, 1000)];
It is not working too...
If you want to scroll TextView text that not Larger then it height . then you can't do this. you wont getting scroll bar if your textView's Text not larger then its frame height.
Setting [txtView setContentOffset:CGPointMake(0, 1000)]; wont work anymore if that data in not longer then textView's height.
UPDATE:-
Here it is i got one trick. If textview not editable and you just want to scroll it try to add your Textview in to one scrollviw. Like Bellow i got this working.
in .h class
#property(nonatomic,retain)IBOutlet UITextView *textvie;
#property(nonatomic,retain)IBOutlet UIScrollView *sceoll;
in .m class
- (void)viewDidLoad
{
[sceoll setContentSize:CGSizeMake(0, 500)];
[textvie setTextAlignment:NSTextAlignmentCenter];
textvie.editable = FALSE;
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
textvie.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
} else {
textvie.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
}
[super viewDidLoad];
}
in xib
and the result is:-
Set the frame of UITextView
CGRect frame = textView.frame;
frame.size = textView.contentSize;
textView.frame = frame;
See: UITextView change height instead of scroll

How to make UITextView so high that it exactly fits the text content on iOS7?

I have a UITextView that's embedded into a UIScrollView. I would like the text view to not scroll and be exactly as high as required to show all of the text.
So the width is fixed and I set the content insets to indent the text a bit.
How do I get the correct height? I tried to set the frame's height to the content height but still it scrolls.
This should do the trick:
-(void)resizeTextView
{
CGRect frame = _textView.frame;
frame.size.height = [_textView sizeThatFits:CGSizeMake(frame.size.width, INFINITY)].height;
_textView.frame = frame;
}

UIlabel shows extra space before text

I have used UILabel in my app. It is working properly in portrait mode. But when I open my app in landscape mode it shows content in center of UIlabel. I have tried sizeToFit but it is not working. As soon as I increase uilabel's width spacing starts to arrive in uilabel.
My code:
self.contentLabel.text = labeltext;
[self.contentLabel setNumberOfLines:0];
[self.contentLabel sizeToFit];
I suspect your UILabel itself and not the text within it is actually aligning incorrectly upon rotation. Make sure the label stays aligned to the top of the view. Try:
self.contentLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
or
self.contentLabel.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
Edit: One other thought.
If auto layout's enabled and you're calling sizeToFit in viewDidLoad, perhaps the view is resizing after auto layout lays out the subviews. Try putting your code in viewDidLayoutSubviews instead.
If you add a UILabel with height bigger than the height of the text, it's normal if that happened and there is no way to change this alignement (Vertical center).
I have two solutions for this problem:
Work with constraint :
This constraint Greater than or equal is just magic.
If you create the label with the code I suggest to work with that:
UIFont *fontReceive = [UIFont systemFontOfSize:[UIFont systemFontSize]];
CGSize sizeText = [text sizeWithFont:fontReceive constrainedToSize:CGSizeMake(260, 9999) lineBreakMode:NSLineBreakByWordWrapping];
Hope that will help!
The best solution is to change the height of your cell according the the amount of text your are populating it with.
Please see this code below as an example.
NSString *content = **YOUR_STRING_LABEL_INTENDED_CONTENT**
CGSize maximumLabelSize = CGSizeMake(390, 1000); //For example - Put in your desired label width here and maximum size etc.
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey: NSFontAttributeName]; //This allows a calculation to be made of the space taken up, so if you're using a custom or large font it will calculate accordingly.
CGSize newExpectedLabelSize = [content boundingRectWithSize:maximumLabelSize options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size;
Now you can change the height of your label by using this next line, where label is the name of the label you made.
GCRect frame = label.frame;
frame.size.height = newExpectedLabelSize.height;
label.frame = frame;
I hope this helps, cheers, Jim.

UITextView text cut off when there is a large amount of text

I have a UITableViewCell subclass that contains a UITextView where scrolling is turned off. I set its frame like this, in my table view cell layoutSubviews method:
CGRect frame = self.bodyTextView.frame;
CGSize size = self.bodyTextView.contentSize;
frame.size = size;
self.bodyTextView.frame = frame;
This has been working fine for some time, but I've noticed that in a case where I have a particularly large amount of text, the text is getting cut off. I've set the text view frame background color to orange so I could verify that the frame was being set correctly. Here is an example (I am only showing the bottom portion of the text view):
The frame is the correct size based on the text (in this case 1019 points), but the text stops before the bottom of the text view. I have also seen the text get cut off part way through a line, (ie the text of the last visible line of text is cut off half way through horizontally). Does anyone have an idea what is happening here?
A few other points:
The text views work well for all my table view cells with shorter content.
If I increase the amount of text in the case shown above, the text view height increases, but the text still gets cut off at the same place.
According to this and similar answers, the problem could be that "the correct contentSize is only available after the UITextView has been added to the view ... Prior to that it is equal to frame.size"
I'd suggest you to calculate the height in a different way, like -sizeWithFont: or -sizeTofit
Use this to get the textSize
// find the text width; so that btn width can be calculate
CGSize textSize = [#"YOUR TEXT" sizeWithFont:[UIFont fontWithName:#"Arial" size:20.0]
constrainedToSize:CGSizeMake(320.0f, 99999.0f)
lineBreakMode:UILineBreakModeWordWrap];
and then set height using this as :
CGRect frame = self.bodyTextView.frame;
frame.size = textSize;
self.bodyTextView.frame = frame;
Hope it helps you.
I had the same issue and resolved it working on the textView padding (in swift5):
yourTextView.textContainerInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
Hope it can help :)

Resources