I'm using XIB to populate tableview of a chat app (messages). It contains a view and a label on it. The label's text continuously changes. How can I find the width of label and then set width of parent view equal to that?
I'm a newbie to swift. So, in this way, is it possible to set height also?
I have set the number of lines to '0' of the uilabel
Assuming that the "Message Text" label is the only subview within the parent view, you could set the parent view's height and width like so:
if let subview = view.subviews.first {
// where `view` is your parent view and `subview` is your text label
view.frame.size.width = subview.frame.width
view.frame.size.height = subview.frame.height
}
Related
I have a UILabel and a UITextView created in storyboard that both change height to fit the text that is passed into them. I need to fetch the height of both of them programmatically in order to size my view correctly (view embedded in scroll view), but when I run this code in their view controller, it gives me the height defined in storyboard rather than the resized height. articleText is my text view, and articleLabel my label.
articleText.attributedText = largeTextString
articleLabel.attributedText = labelString
print(articleText.contentSize.height)
print(articleLabel.bounds.size.height)
// Both return storyboard value, not resized value
Thanks in advance!
I am creating a chat interface and have a UITextview and button for sending message inside a UIView. The height of UITextView changes based on its content size but UIView height does not change with it. Chat view
I will appreciate any help on this.
Here are constraints on the message field Message field constraints
If you programmatically created the view:
containerView.frame.size.height = textView.frame.size.height
If you create the view in the storyboard
Create a height constraint for your view. Then connect that constraint to your code. Then run this:
heightConstraint.constant = textView.frame.size.height
Set autolayout constraints specifying a fixed space and pin them from your UIView top to the UITextView top and from your UIView bottom to the UITextView bottom.
It is unclear from your question how you are setting the height of your container view and your text view. You should have a height constraint on the text view and change its constant when you want to resize the text view. Do not change the frame directly. The container view must not have a height constraint - it should resize solely because of the bottom and top constraints to the text view.
without auto layout code:
txtView.frame = CGRectMake(view.frame.origin.x + 8, view.frame.origin.y + 8, view.frame.size.width - 100, view.frame.size.height - 16);
btnSend.frame = CGRectMake(view.frame.size.width - 92, (view.frame.size.height/2) - 25, 84, 50);
I have one superview in that view there are two subview.
I have set equal height of that two view. Those subview height changing respect to label height in it.
This is first screen that i have implemented. It works when label 2 height is greater then label 1 height changes with respect to label 2 height.
This is first screen. It work properly but another situation it is not working when label 1 height is greater than label 2 height then it's not working expected.
output like following screen
As expected label 2 need to adjust with respect label 1 but then also label 1 is adjusted with respect to label 2.
so how to apply constraint for set equal height for two view depends on their subview height.
You can make your label heigh dynamically
Call a method for UILabel height
-(CGSize) getContentSize_Label:(UILabel*) myLabelView{
return [myLabelView sizeThatFits:CGSizeMake(myLabelView.frame.size.width, FLT_MAX)];
}
and set label heigh like
myLabelView.frame = CGRectMake(myLabelView.frame.origin.x, myLabelView.frame.origin.y, myLabelView.frame.size.width, [self getContentSize_Label:myLabelView].height);
and set
myLabelView.numberOfLines = 0;
What you are missing is most probably equal height constraint.
Select two container views of the labels and then go to Xcode/Editor/Pin/ and select Height equally.
I've just checked it and its working for me.
I have a screen layout where there are two resizable labels , which will contain multiline text. These labels are placed inside their parent views which intern are added to main contentView, main contentView is then added to scrollView ( thats what most of the solutions suggests). For both the labels (below About and Time and location labels in first image attached) I have set height constraints as "greater than or equal to" and setting the numberOfLines to 0 as well as calling SizetoFit, but actual output is not as expected (see second image attached). There are no constraints warnings. All constraints are provided for all the elements.
The code in viewDidLoad is as follows for one of the label.
self.lblAbout.text = #"this is a long two three lines about string which will have two lines this is a long two three lines about string which will have two lines";
self.lblAbout.numberOfLines = 0;
[self.lblAbout sizeToFit];
[self.lblAbout setPreferredMaxLayoutWidth:244.0];
Also
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
scrollView.contentSize = contentView.frame.size;
}
Not if any additional constraints are needed, I have added all leading , trailing , top , bottom constrains along with height wherever needed, plus the spacing between all the views is in place.
What i want is the labels should get adjusted to number of lines and the contentView (parent view) should scroll inside ScrollView as the total height will be larger than the screen available.
*** problem I think is the outer view of the labels aren't getting resized as per the label because of which all the views below it aren't getting repositioned ****
Please try this Solution,
1. add Height Constraint to superView of your Label.
2. add IBOutlet of that Constraint
3. add this Method to find out Height of your Text
you need to give width of super view of your Label so width will be same as your super view
4. Now get Height form returned CGRect and assign it to your Constraints's constant. it should be like
heightConstraint.constant = youObject.size.height;
please Make sure you have added other Constraints accordingly this. if not than you need to also increase Height of other superviews accordingly.
(CGRect)sizeOfDetailLabelFromString:(NSString*)string maxWidth:(CGFloat)maxWidth{
NSDictionary *attributes = #{NSFontAttributeName:FONT_LIGHT};
CGRect rect = [string boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attributes context:nil];
return rect;
}
Here is what worked finally.
Added ScrollView in main View
Added View as a contentView ( be sure to rename it in designer to something than just view )
pinned scrollview to main view using leading trailing top and bottom space constraints.
Pinned contentView to scrollview same as above
Added all the components with their respective constraints
Set the height of UILabel which i want to resize using "Greater than or equal to constraint (this is necessary) and set number of lines to 0 in code
The parent view of label shouldn't have any fixed height but enough constraints to calculate it at runtime.
Make sure ScrollView has no ambiguity in calculating contentSize.
imp - Add constraints to width of Main view , scrollview , contentView ( that was in my case , you may not need equal width constraint between contentView and scrollview , but between contentView and main view its necessary)
Now scrollview scrolls exactly the way it's needed.
To my belief most of the above things i already did but somehow it wasn't working ,deleted everything and did all the things again few times and it worked.
I have a UIView in top of UITableView (drag and drop in storyboard . I tried to hide and show that view. but table view stands in place . how can i change frame of uivew so when it is invisible tableview goes all the way to top?
PS. I will provide more info for those who are willing to help. Thanks in Advance
http://i.stack.imgur.com/cM3tA.png
![the view with hamburger button and textfield)
With autolayout:
Add height constraint to your UIView with button and textField
Create Referencing Outlet for this height constraint (lets count it will be "searchViewHeightConstraint")
Add Vertical Spacing Constraint between the UIView and the UITableView
In code - when you need to hide UIView - just set its height constraint to 0
self.searchViewHeightConstraint.constant = 0.f;
Also you need to store somewhere the initial height of the UIView (when it is visible) and set its Height Constraint to this value when you need to show UIView.
Without autolayout:
Save the value of UIView frame height
Set the height of UIView to 0
Decrease the y-coordinate of UITableView frame with saved height of UIView
CGRect tableViewFrame = tableView.frame;
tableViewFrame.origin.y -= viewInitialHeight;
tableView.frame = tableViewFrame;