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];
Related
I config a textView in a UIViewController like following:
textView configuration
but when controller viewDidAppear I found that UITextView's contentSize = {375, 242} and UITextView can not scroll.
But if i tap the textView, let the textView begin editing (but edit nothing), then i touch the controller's view let textView endEditing, log the textView, this time contentSize = {375, 361} and UITextView can scroll.
Is anybody know why? Thanks.
You can add textView something like,
UITextView *standardTextView = [[UITextView alloc]initWithFrame:CGRectMake(20, 50, self.view.frame.size.width - 40, 120)];
standardTextView.layer.borderWidth = 1.0;
standardTextView.layer.borderColor = [[UIColor lightGrayColor]CGColor];
standardTextView.delegate = self;
standardTextView.font = [UIFont fontWithName:#"Helvetica" size:17.0];
[self.view addSubview:standardTextView];
then when your content(i.e text) will be bigger than textview's height it will enable scroll otherwise it remains disable!!
NSTextContainer has a property called heightTracksTextView which may be interfering with your setup here, as the default is false.
I would double check the height of the NSTextContainer after you initialize the UITextView and after you add the UITextView to the view hierarchy.
Check the documentation here: https://developer.apple.com/reference/uikit/nstextcontainer/1444559-heighttrackstextview
If that doesn't help, let me know, I know I've solved this issue before, but I'm not at my computer right now.
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
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;
}
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.
I have a UIScrollView that includes some components, and one of these components is a UITextView, what I want is to let the UITextView dynamically expands with the UIScrollView, in fact I use autoLayout, so this code is not working:
CGRect frame = self.detailTextView.frame;
frame.size.height = self.detailTextView.contentSize.height;
self.detailTextView.frame = frame;
scroll.contentSize = CGSizeMake(scroll.contentSize.width,
300 + self.detailTextView.frame.size.height);
[self.detailTextView setFrame:frame];
What I want is to help me doing this with storyboard elements.
Instead of setting the frame, drag one of the height constraints for your text view into your controller to create an outlet. Then in viewWillLayoutSubviews grab the contentSize and set the constant of your text view height constraint. That should accomplish what you're doing with the frame in your code above. Just be sure that your text view has constraints from all edges to your scroll view so that the scroll view can size itself properly.
in my case, what I did is kept the scrollview as super view and added all constraints for the subviews and the uitextview has constraint from all edges to the scrollview. then updated the content size in -viewDidLayoutSubviews method. here is the code snippet:
-(void)viewDidLayoutSubviews{
NSDictionary *attributes = #{NSFontAttributeName:[UIFont systemFontOfSize:12.0]};
CGRect rect = [_detailText.text boundingRectWithSize:CGSizeMake(_detailText.frame.size.width - 10.0, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:attributes
context:nil];
CGRect frame = _detailText.frame;
frame.size.height = ceil(rect.size.height) + _detailText.textContainerInset.top + _detailText.textContainerInset.bottom;
_detailText.frame = frame;
_detailText.contentSize = CGSizeMake(_detailText.frame.size.width, _detailText.frame.size.height);
[_contentScrollView setContentSize:CGSizeMake(_contentScrollView.frame.size.width, _detailText.frame.origin.y + _detailText.frame.size.height)];
}