I have a text view inside a scroll view, I want to let the height of text view be dynamic as its content, because I don't want to scroll the text view alone, In fact I want to scroll the text view with the scroll view together.
In fact, I confused with the right menu of the image above, about the constraints height equals, less than, bigger than,...
I don't want to let the height stable, I want it dynamic, in my code:
CGRect frame = self.detailTextView.frame;
frame.size.height = self.detailTextView.contentSize.height;
self.detailTextView.frame = frame;
scroll.contentSize = CGSizeMake(scroll.contentSize.width,
200 + self.detailTextView.frame.size.height);
[self.detailTextView setFrame:frame];
which detailTextView is the name of text view, and scroll is the name of the scroll view.
But it doesn't work properly, what shall I do please help!
Note: Is there any relation between this issue and the checkbox of (use AutoLayout), because it is checked in my application
What the problem is? I have seen only one issue that you need to disable the scrolling of youtTextView.
CarinaM
Use following method for getting dynamic height of textfield as per text.
-(CGFloat) GetHeightFoText:(NSString *)aStrTxt FoWidth:(int)aIntWidth ForFontSize: (int)aIntFntSize
{
CGSize maximumLabelSize = CGSizeMake(aIntWidth,9999);
CGSize expectedLabelSize = [text sizeWithFont:[UIFont systemFontOfSize:aIntFntSize]
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
return expectedLabelSize.height;
}
Related
I have dynamic no of textviews and their size can also be dynamic, after each text view there are also dynamic no of labels, and each item is place on scroll view, so that scroll view also has dynamic size, So Someone guide me how to accomplish this task?
forgive me if this is repetitive question plz!
For setting dynamic height of UILabel or UITextView, you can implement following method
This example is for UILabel, Remember, you need to set noOfLines property before setting dynamic height, you can set noOfLines to max number.
NSString *text = #"Your text here";
CGSize size = [text sizeWithFont:lblName.font constrainedToSize:CGSizeMake(lblName.frame.size.width, 10000)];
scrollview.contentSize = CGSizeMake(scrollView.frame.size.width, size.height);
Hope this helps
What you need,
1 calculate textsize which you are going to to show on differnt controls.
for this use this line
[titleString sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(285,9999) lineBreakMode:UILineBreakModeWordWrap];
2 Also use labels instead of textView at each place if you only need to show text.
because textViews justify the text means your same line can be fit in one line but also in two lines
3 set the scrollView contentSize as above answers says.by adding all textSizes with consider some spaces between various controls.
you can set the size of scrollview using setContentSize: and query size [scrollView contentSize]
Let's say I have two UILabels positioned vertically below each other in a UITableViewCell. The line break mode is set to UILineBreakModeWordWrap for both.
Their horizontal size is fixed, but both can stretch vertically based on how much text they display. How do I position the one that's below dynamically so that they would never overlap?
i use this function to get the height of the text, and then set the second label height to the result.
- (CGFloat)HeightOfText:(NSString *)textToMesure widthOfLabel:(CGFloat)width
{
UIFont *textFont = [UIFont systemFontOfSize:16];
CGSize ts = [textToMesure sizeWithFont:textFont constrainedToSize:CGSizeMake(width-20.0, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
return ts.height+25.0; //you can change the last number to fit the space you wish to have between the labels.
}
and you use it like that:
NSString *firstLabelText = #"the text";
CGFloat textSize = [self HeightOfText:firstLabelText widthOfLabel:firstLabel.frame.size.width];
then use "textSize" to set the second label height.
hope it will help.
Try -[UILabel sizeThatFits:]. Say you're laying the labels out in a 300px wide column, you can pass in a size of (300,99999), and it'll tell you the size it actually needs to fit that text in. You can use that to adjust the frames of your labels appropriately.
I've done something similar by resetting the frame of the view below (assuming you've already done sizeWithFont or sizeThatFits and resized the labels). Similar to this:
- (void)positionView:(UIView *)aView belowView:(UIView *)anotherView withPadding:(int)padding
{
CGRect aFrame = aView.frame;
if (anotherView)
{
aFrame = (anotherView.frame.origin.y + anotherView.frame.size.height);
}
aFrame.origin.y += padding;
aView.frame = aFrame;
}
I have a UITextView and I would like to only allow it to be scrollable horizontally only.
Basically when the UITextView word-wraps I want the user to have to scroll horizontally to be able to view the rest of the contents.
You can set the content size of the text view to the height of the view. Just make sure the width of the content extends past the width of the textView's frame or else it won't scroll.
// UITextView* myTextView: gets declared somewhere...
// The text view (subclass of a UIScrollView) won't go past its content size
[myTextView setContentSize:CGSizeMake(width, myTextView.frame.size.height)];
// Just in case, if you don't want your user bouncing the view up/down
[myTextView setAlwaysBounceVertical:NO];
I hope that's what you were looking for.
The solution is to turn off all the scroll options on the UITextView itself, then embed it in another UIScrollView. You can get actual code by searching on this term "DualScrollTextView" in google. You cannot force changes to contentSize to accomplish this - many have tried.
This worked for me:
self.dishNameLabel.text = #"Here is some very longgggg text to test with";
float width = ceil([self.dishNameLabel.text sizeWithAttributes:#{NSFontAttributeName: [UIFont fontWithName:#"WorkSans-SemiBold" size:15.0]}].width);
self.dishNameLabel.frame = CGRectMake(self.dishNameLabel.frame.origin.x, self.dishNameLabel.frame.origin.y, width, self.dishNameLabel.frame.size.height);
self.dishNameScrollView.contentSize = CGSizeMake( width, self.dishNameScrollView.frame.size.height);
Of course, you will have to replace your text and font with your respective specs
By default, the UITextView's contentView becomes scrollable when there is too much text to fit into the textview based on it's height.
I'd like to disable this and instead, update the height of the UITextView to fit in the text. The reason I'm doing this is because I'm adding the UITextView as a subview of a UIScrollView which should handle the scrolling, much like in the native Mail app (when you enter text, the whole view scrolls up, not just the textview.
Anyone got some ideas / has run into the same problem before?
It is very simply done like this:
CGRect frame = textView.frame;
frame.size = textView.contentSize;
textView.frame = frame;
This should set the height of the textview to appropriately fit all of its content.
Few little changes:
-(void)textViewDidChange:(UITextView *)textView {
CGFloat fontHeight = (textView.font.ascender - textView.font.descender) + 1;
CGRect newTextFrame = textView.frame;
newTextFrame.size = textView.contentSize;
newTextFrame.size.height = newTextFrame.size.height + fontHeight;
textView.frame = newTextFrame;
}
Adding the font height gives room for the autocorrection box when you spell something incorrectly.
The UITextView should also be set to not scroll:
[aTextView setScrollEnabled:NO];
How can you make a UITextView expand with the text that is inside of it?
you could try this...
UITextView *textView; // your UITextView
NSString *text; // the text that you want to place in the UITextView
UIFont *textViewFont; // the font that you are using for your UITextView
CGSize size = {255,2000.0f}; //The default width, and max height you want to use
CGSize newSize = [text sizeWithFont:textViewFont
constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
textView.frame = CGRectMake(0,0,newSize.width, newSize.height);
You can try this..
CGRect frame = textView.frame;
frame.size.height = [textView contentSize].height;
textView.frame = frame;
Are you using UITextView inside UITableView?
Let's suppose you are using UITextView inside UITableView. In order to make UITextView height dynamic(As per the content inside it) we need to take care of following things:-
Make sure UITextView constraints are properly given. e.g: I had used XIB for tableview cell and inside cell I had a UITextView. So, I will give constraint to UITextView 0 to all four sides e.g: Top, bottom, leading, trailing. (Depends on your requirements).
Make sure UITextView attribute, autoscroll is disabled. It should be off.
Make sure TableView cell height is UITableView.automaticDimension
Now inside your 'textViewDidChange' delegate method, Just add this below mentioned code:
UIView.setAnimationsEnabled(false)
tableView?.beginUpdates()
tableView?.endUpdates()
UIView.setAnimationsEnabled(true)
Now your textView will auto expand, while you are typing init.