my UIView has a UILabel and a UIWebView inside, the UILabel is in the top, the UILabel has the height variable:
theTitle.text = [NSString stringWithFormat:#"%#", noticeTitle];
theTitle.lineBreakMode = UILineBreakModeWordWrap;
theTitle.numberOfLines = 0;
[theTitle sizeToFit];
how can I start the UIWebView below the UILabel?
Thank you in advance.
Regards.
I think you want your webView to be displayed just below the label.If the string you want to display is dynamic then your Label size must also be dynamically set according to the string size. So for adjusting your label size Just follow the link :
Adjust UILabel height depending on the text
And then you can get the height of your label by :
int height = theTitle.frame.size.height;
You then set your webView frame according to this height + yValue of label frame.
Related
I have a UIScrollView and I take a UIImageView and an UITextView that scrolls vertically.
The scrollview is 320*700. I want to do the scroll size depend upon the UITextView size, The image is same size but the text changes depend on the content.
How can I setup the scrollview's size to change only when I've got more or less text in the UITextView?
You can set your UIScrollView's size dynamically according to the size of your UITextViewusing something like
- (void)viewDidLoad
{
float sizeOfContent = 0;
UIView *lLast = [scrollView.subviews lastObject];
NSInteger wd = lLast.frame.origin.y;
NSInteger ht = lLast.frame.size.height;
sizeOfContent = wd+ht;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, sizeOfContent);
}
Please also refer to How to set content size of UIScrollView dynamically to be able to understand better .
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.
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 custom uitableviewcell with several labels and I would like for some of them to autoresize their frame (width) based on the content (text). I am not sure how to accomplish that. I tried to set fixed frame of the label and after apply autoresizingMask, but that doesn't do the trick. Any *pointer to a sample?
If you want to adjust the frame of a label to fit the labels text, just call
[label sizeToFit];
This will fit in both dimensions.
Use the following method:
CGSize *size = [label.text sizeWithFont:fontOfLabelText];
float widthOfLabel = size.width;
size.width will return the actual width that the text in the label will occupy on the screen. Set the label width equal to width of text.
If you just want to make so that the label fits in multiple lines, you have to say:
cell.textLabel.numberOfLines = 0;
However I'm not sure if this is the one that you want... Usually if the content is text than that'll be what you want.
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.