scrollView = [[UIScrollView alloc] initWithFrame:
[[UIScreen mainScreen] applicationFrame]];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(25, 100, 200, 250)];
textView .text =#"long text ";
I will be getting some data of unknown length. to be added to the UITextView . Sometimes the height of the content of the text might exceed the height of the UITextView which is 250 (shown above).
How can i increase the height of the UITextView based on the text i receive ? I would appreciate a sample code? Later i need to add this to a UIScrollView (i know how to add to a scrollview), but again i don't know how to increase the height of the scrollview. Can someone help me code this ?
note: according to my code i am defining the width and height of the UITextView before adding the text to it.
UITextView is a subclass of UIScrollView. When the text content grows larger than the frame of the text view, the content will automatically begin to scroll. There is no need to embed a UITextView in a UIScrollView because it already is one (and more!).
Set the size of textView equal to it's contentSize.
The scrollView you have to set it's contentSize equal to the size you want.
Related
The above is what I get on the simulator regardless of the frame I set for the UITextView. The above UITextView is a subview to UIScrollView (the light gray view behind). And this UIScrollView has been presented modally. No matter what frame I set for the UITextView, its height remains as shown above. What could be the problem?
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 540, 620)];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 520, 80)];
textView.contentInset = UIEdgeInsetsZero;
textView.layer.borderWidth = 1.0f;
textView.layer.borderColor = [[UIColor blackColor] CGColor];
[scrollView addSubview:textView];
Thanks!
Noted one more thing. Changing the width of the textview from say the above 520 to 220 does indeed result in a change of width and it shows. But no matter what the value of height is, the textview keeps displaying with the same height as in the above image.
your UITextView size is dependent on the Height and width of UIScrollView first increase the size of UIScrollView size than increase th eUITextView size.
I'm getting the same issue and when I try to po the frame height it give me:
property 'frame' not found on object of type 'UITextView *'
This is after installing the new ios6 dev kit... so maybe that's it...
Yup that's it. If you go to file inspector in your xib or storyboard and untick "use autolayout" then that will help.. But's it's screwed everything, so I'm just going to make the damned uitextview programatically!
How would I go about setting the content size for a scrollview, when the content size is dynamic. I have added all my content to a UIView named "contentView", then try calling the setcontentsize as below, but this results in no scrolling.
sudo'ish code:
[scrollView setContentSize: contentView.frame.size];
Maybe "contentView" is not stretching its size to fit its children?
Any help would be appreciated.
UIView or UIScrollView will not auto stretch based on content. You have to manually calculate the frames and position it accordingly inside the scrollview and then set the contentSize of the scrollview to the biggest possible size that can hold all its subviews.
This depends on the type of content you are going to add dynamically. So let's say you have a big text data to show, then use the UITextView and as it is a subclass of the UIScrollView, you can get the setContentSize of TextView when you assign the text content. Based on that you can set the total size of the UIScrollView.
float yPoint = 0.0f;
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, yPoint, 320.0f, 400.0f)];
UITextView *calculatorTextView = [[UITextView alloc] init];
calculatorTextView.text = #"My looong content text ..... this has a dynamic content";
[calculatorTextView sizeToFit];
yPoint = yPoint + calculatorTextView.contentSize.height; // Bingo, we have the new yPoiny now to start the next component.
// Now you know the height of your text and where it will end. So you can create a Label or another TextView and display your text there. You can add those components as subview to the scrollview.
UITextView *myDisplayContent = [[UITextView alloc] initWithFrame:CGRectMake(0.0f, yPoint, 300.f, calculatorTextView.contentSize.height)];
myDisplayContent.text = #"My lengthy text ....";
[myScrollView addSubview:myDisplayContent];
// At the end, set the content size of the 'myScrollView' to the total length of the display area.
[myScrollView setContentSize:yPoint + heightOfLastComponent];
This works for me.
When you add stuff to your contentView call [contentView sizeToFit] and then the content view will stretch to fit its subviews, then, the code you post will work.
hello every buddy
i want to make horizontal image scroller at bottom view and in the back of side big Image View. i don't know how to make image horizontal scroll.
Place the image view inside a scroll view whose horizontal content size is greater than its frame width, i.e.
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = CGRectMake(0, 0, 100, 100);
scrollView.contentSize = CGSizeMake(300, 100);
[scrollView addSubview:imageView];
You should use contentSize property of the UIScrollView to set its content size according to your requirement and then set the property showsHorizontalScrollIndicator to YES and showsVerticalScrollIndicator to NO . But you don't require to set these two if you set frame and content size properly. Like if _myScrollView.frame.height and _myScrollView.contentSize.height is same then you don't need to set horizontal and vertical scroll. Its vertical scroll will automatically be disabled.
you can use scroll view with page control for a horizontal slideshow. follow the tutorial it might help you
http://www.edumobile.org/iphone/iphone-programming-tutorials/pagecontrol-example-in-iphone/
What is the best way to display a large chunk of text (taken from a .txt file in the app) in a UIScrollView so a user can scroll about it? Length of the text is variable.
On Interface Builder open the Attributes Inspector (if not already open - command-1) and uncheck "Editable".
Also notice there's a Scroll View section below. Make sure "Scrolling" is checked.
Hope this helps somebody (the post is a year old so I guess by now the one who posted it doesn't need this info).
I came here looking for an answer and found that all answers are bad - or flat out wrong.
The proper way to do this is using UITextView by itself. Since it is a descendant of UIScrollView, it has scrolling built-in and lots of features for adjusting formatting such as the insets etc.
If you intend to only show text, you need to explicitly disable editing. You do this by setting the "editable" property to false.
And if you want to disable the text selection mechanism, set the "selectable" property to false.
In newer versions of iOS, UITextView has added support for NSTextContainer which gives you even greater control over formatting.
One way I had working for me is to create UILabel, set text and then set content size of scrollview by it size.
Here is an example
Quote:
// alocate and initialize scroll
UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
// alocate and initialize label
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
// add long text to label
myLabel.text = #"Lorem ipsum... long text here";
// set line break mode to word wrap
myLabel.lineBreakMode = UILineBreakModeWordWrap;
// set number of lines to zero
myLabel.numberOfLines = 0;
// resize label
[myLabel sizeToFit];
// set scroll view size
myScroll.contentSize = CGSizeMake(myScroll.contentSize.width, myLabel.frame.size.height);
// add myLabel
[myScroll addSubview:myLabel];
// add scroll view to main view
[self.view addSubview:myScroll];
Usage of the UITextView into the UIScrollView. I could not recommend this because UITextView is the subclass of UIScrollView. Apple is also recommending the same.
Use UILabel in this case as a sub-view,
Put the UITextView into the UIScrollView.
I already did several searches on Stack Overflow and Google, but I couldn't find a solution to my problem.
I have a Detail View for a product that includes a UIScrollView and a few subviews (UIImageView, UILabel, UITextView). You can find an example here.
First I wanna autoresize the UITextView (not the text itself!) to the corresponding height. Then I wanna autoresize the entire UIScrollView so that it fits the height of my UITextView and the elements above it. I've tried the following code:
myUITextView.frame = CGRectMake(2.0, 98.0, 316.0, self.view.bounds.size.height);
[scrollView setContentOffset:CGPointMake(0.0, 0.0) animated:NO];
scrollView.contentSize = CGSizeMake(320.0, 98.0 + [myUITextView frame].size.height);
[self.view addSubview:scrollView];
98.0 + [myUITextView frame].size.height) because my thought was: After getting the height of myUITextView, add the height of the other subviews (98.0) to it.
Unfortunately it doesn't work very well. Depending on the content of the UIScrollView, it is too short or too long. I did some logging with the result that the height of the UITextView is always the same:
2010-01-27 14:15:45.096 myApp[1362:207] [myUITextView frame].size.height: 367.000000
Thanks!
There is actually a very easy way to do resize the UITextView to its correct height using its contentSize.
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
One thing to note is that the correct contentSize is only available after the UITextView has been added to the view with addSubview. Prior to that it is equal to frame.size
A UITextView won't automatically resize itself to it's contents (not as far as I know anyway) so you need to get the size of the text yourself and size the UIView accordingly.
The functions on this page will help - you can use them to get the width and height of a string, something like
// Get the size that the string will render at
NSString *contents = #"This is the content of your UITextView";
CGSize areaSize = [contents sizeWithFont:myView.font forWidth:myView.frame.size.width lineBreakMode:UILineBreakModeWordWrap];
// Then set the frame of your UITextView to be the right size
myView.bounds = CGRectMake(0, 0, areaSize.width, areaSize.height);
Then, you can layout the other components around it.
Hope this helps,
S
PS Warning, the link to the docs is correct but my code example is untested, sorry :)