Changing the height of a tableview cell dynamically - ios

I'm implementing a tableview that look like facebook newsfeed.
I've done setting the height of tableviewcells dynamically based on the content inside
But now i got a problem that is i want to show ....more after just showing 3 lines of content just like in facebook and after clicking the more button the height of the tableviewcell has to be increased by showing the full content of the tableview cell.
See the screenshots below
instead of showing all the content i want to show just 3 lines and after that ....more has to be there and if the more button is clicked, the height of the tableviewcell has to change dynamically

You could truncate your UILabel or change the size of your UITextView and place an adjacent UIButton with the title "more". So when the user clicks on more you would change the height of the cell and the frame of your textView/label and the more button should be hidden.
You could show a UILabel with test using truncating it( Your Text is... ) along with a UIButton place just below that.
So when user would click on the button you could change the property of the label( no. of lines, word wrap, etc) to fit in all the text along with changing the height of cell.

You can calculate text hight using :
-(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
Returns the size of the string if it were rendered and constrained to the specified size.
Parameters:
1) font : The font to use for computing the string size.
2) size : The maximum acceptable size for the string. This value is used to calculate where line breaks and wrapping would occur.
3) Return Value : The width and height of the resulting string’s bounding box.
According to this you can update cell height.

Related

How to auto adjust cell’s heights depending their text inside?

I am using a tableview to display a list of articles. Each cell needs to show an image and a text that has a brief descriptions of articles. What I want is to adjust the cells height depending the description length.
I know that I can resize the cells with the delegate method heightForRowAtIndexPath but I still don’t know which height to return.
Any help?
Ok, you can do that just using Interface builder. On the Tableview properties click “Row height” automatic.

Then you can use a UILabel with property lines set to 0 to display your text. When adding the constraints to the cell you need to make sure that the height of cell depends on the intrinsic content size of the label, basically is the size of the content (the text inside the label).
For example:

Here I have added a UIImage with constant height and width and with top and leading space to 0. Also I added a UILabel with top/bottom/leading/trailing space to 0. Here the cell height will depend on the UILabel intrinsic size that is what you want. Also to prevent glitches with short texts you can add a minimum height size to the UILabel.
Of course this is an example, you can achieve the same using other constraints. The main thing here is to be aware of the concept of intrinsic content size: a predetermined size of a view based in its content.
Let's say you have a textView in each cell. The width of the textView is fixed. And you know the text for each cell. Base on the info above, you can get the height of your textView by:
textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
Then you should be able to calculate the height of your cells base on your layout and stuff.
The question is answered but just in case you want to know... You can return UITableViewAutomaticDimension in heightForRowAtIndexPath.
Nevertheless you should google more before asking a question that has been answered multiple times on the web (and on stackoverflow) ;)

Is it possible to get part of tableview cell to disappear by reducing it height at tableview delegates

I have a table view cell which has three elements vertically
a title label
a text box
a validation label
I want to show validation label only when it fails.
If I just hide the validation label then the space occupied by the label is stil there and it looks awkward having more distance between the next row
so what would be the best approach to handle the scenario
Change the table-row height whenever validation label's hidden status changes
I think this approach probably will not work because label is still there in the tableview cell so the tableview cell will readjust itself to have the label height there irrespetive of whether it appears or not
Change the label height constraint constant and tablerow height whenever there is change in label's hidden status
so basically if I want to hide it then I will be setting the label's heigh to zero and readjust the tablerow height ... it'll work but there is a problem..
If I set label number of lines zero then depending on the content label will change its content height .... but as I am using constraint for height... I will be hardcoding the height and it can lead to problem again.
Basicallyy my need is I have one label and text box whose height is fixed and I have a one more label ... this could be hidden sometimes and this could be one line or 4 lines
I want the row height to adjust according to that(0 lines(hidden),1/2/3 line label visible)
sometime even the first label that I was talking it could go to the next line in smaller devices like iphone 4s
so somebody help me out on the issue

iOS Swift Text Fill Screen

I have taken a label and dragged it onto a blank UIView.
I have changed the text orientation with
codeLabel.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
but I now want the text to fill the screen, I have browsed the web and found this but it doesnt seem to work
codeLabel.adjustsFontSizeToFitWidth = true
Add a width equal constraint between the UILabel and its parent UIView. Do that by draging from the UILabel to the UIView.
What you want to achieve is to let the UILabel with equals to the UIView height. but what you have so far is an equal widths constraint.
Select that constraint by clicking it and change the value next to "Second Item" to Superview.Height.
To let font fit the screen:
select the label.
set a very large font. example 100.
change the value of Autoshrink to Minimum font size.
set a small value there. example 5.

Swift: UILabel how to show all text

I am very new to Swift and I don't know how to make my UILabel show all text.
Here is how my label looks like in my storyboard:
As you see, part of the text is omitted. I can show all the text by dragging the UILabel to resize. However, during the runtime, if the text is set dynamically, how can I resize the label so that it can show more all of the text?
I tried UILabel.sizeToFit() but that doesn't seem to change anything.
you can do this by setting numberOfLines to 0
Example
myLabel.text = "whatever"
myLabel.numberOfLines = 0
myLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
And you have so set frame according to it
Refer : to this link
YourUILabel.sizeToFit() is right but that is not all of it..
You need to set the number of lines and the kind of line break before calling .sizeToFit()..
YourUILabel.numberOfLines equal to 0 for unlimited number of
lines...
YourUILabel.lineBreakMode equal to NSLineBreakMode dot(.)
ByWordWrapping for example..
If you are not resizing the UILabel frame using autolayout you can change the Autoshrink option by selecting your label from the storyboard and adjust this option on the inspector pane.
.
Why do it the old way.. Let's use autolayout. If you haven't used it yet. Lets begin - Steps :
Create a UIlabel and set it's width to any amount you want it on screen.
Select the label go to Editor menu -> Pin and one by one pin Leading Trailing and Top space to Superview (not the bottom one).
Since you have fixed width and you have dynamic height so also pin Width.
Go to Attribute inspector tab and set lines to 0 (means unlimited).
Go To size inspector and set Content Hugging priority(250/251) to low and Content Resistance priority to high (say - 750).
And you are done.!!(not a single line of code)
You first have to calculate the Height of label according to the length of the Text you want to set and then accordingly increase the number of lines
as
yourLabel.numberOfLines = "Your calculation number"
and then Set the Frame of the Label according to the Height calculated
and
if you cannot calculate the height of the Label then i would say use the TextView it will automatically add a scroll to the text and you can view all the text.

UILabel alignment to other UILabels

I have a cell prototype that i'm trying to add two labels to. I have the two labels right next to each other, but the size of the labels are dynamic, so I want the second label to be able to shift depending on the size of the first label.
Basically, I want there to be a fixed gap between the two labels, but the two labels' sizes are dynamic.
How do I do that?
EDIT:
Actually I found out how to do it via Storyboard. If you select the two labels you want to have a fixed gap between, just command select both of them and then go to the corner of storyboard and click the pin menu, which is that little 'H' looking thing in the group of buttons near zoom in/zoom out in the bottom right corner of the storyboard screen.
Get the label size by this method:
- (CGSize)sizeWithFont:(UIFont *)font
https://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html.
Then set label's textAlignment to NSTextAlignmentLeft and NSTextAlignmentRight, and set frames by the string size and other offset.
UIView -sizeThatFits: and -sizeToFit will allow you to manually calculate a position for the second label. This is slightly more accurate than using the NSString method, as there's more to a UILabel than just the text — this will respect content insets, etc.

Resources