iPhone TableView: defining padding between images and text in a cell - iphone

does anybody know how to vertically align these text elements. I would like to define an absolute left padding for the text which is not affected by each image dimensions. It's quite annoying like this:
thanks a lot!

You have two options.
1) UITableViewCell has a textLabel property which you can access and adjust the frame. See the Apple docs for more info on UITableViewCell
2) You can ensure that all of your icons have the same width.

You could do it by slecifying the frame on your UIImage, UILabel, or both. Set the float x to a safe distance from the left on the label or specify the width of your widest image as the width of the UIImage frame.
I can clarify more if needed when I'm not on my mobile.

Related

Dynamically sizing UITableViewCells

I have worked with dynamically sizing UITableViewCell before. But currently I am facing a problem statement where I am unable to achieve dynamic sizing using autolayout
My cell has three components viz-a-viz one UIImageView, two UILabel
The structure of the cell is as follows
UIImageView has to be of fixed width and height in my case 60X60
UIImageView will be vertically centered inside the UITableViewCell
UIImageView is having a leading constraint of 15 from the UITableViewCell
Now the two UILabels are vertically placed
Both the labels can have dynamic height and I want them to push expand the cell using autolayout but I am only able to achieve keeping the top UILabel of constant height and the below UILabel increases in height.
I want both the UILabel to expand vertically and increase the size of UITableViewCell.
Just use StackView for both label and make both labels numberOfLine to 0 and line break should be WordWrap. give leading,trailing,top and bottom constraints to stack view. for spacing between labels give spacing for stackview on xcode detail pannel.
See the Screenshots for the setting of the constrains.
Add a height constrain to the label with the constant greaterthanEqualTo a fixed value (see screenshot 2). and set the lines to 0. In the table view heightForRowAt indexPath set that to UITableViewAutomaticDimension .

How can i resize UIView for an amount of UILabels and resize UILabels to fit text?

I have an UIView that it should contains UILabels, and these UILabels are not fixed if an UILabel is nil, i don't display it, and if is not i must resize the UILabel to fit text (they can be 2 ligne) and put it under another UILabel, and after i must resize the UIView container for the amount of this UILabels. this the Screen Shot that i want to do programmatically:
UILabels can fit large amounts of text by automatically making the font smaller (see the minimum font size value in the Interface Builder). Instead of labels, I would use UITextViews. They fit large amounts of text by allowing the user to scroll. To make them hidden, use change the hidden selector attribute of the UITextViews. Because the UITextViews will never change in size, you can easily calculate how much to decrease the height of the UIView container by per UITextView. Hope this helps!
I think you have to calculate the height of the labels (more specifically, the addition from the original height) one by one. Of course you should be able to do this systematically by having all the labels in an array.
You can use [NSString sizeWithFont:] methods (there are several with similar names) to calculate the height.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html
If you want to keep the same font size then you can calculate the frame size for the label at run time depending upon the length of the text..
See here
You need to used following two properties of label-
lineBreakMode
numberOfLine
when you use both of these properties then you will achieve what you want.

How to configure iOs UITextViews and UILabels so that their height is determined by their content

I am basically trying to find the correct way to design a UITableViewCell that has two UITextViews and three UILabels inside it. The two UITextViews have dynamic content and their size cannot be determined until runtime.
I have tried various methods of recalculating the height of my UITextViews based on the content that is loaded in them at runtime, and though very laborious and inane, I have gotten that to work, but they then bleed over the UILabels positioned in the xib below them.
Do I really have to recalculate the y coordinates of every UILabel after calculating the new size of the UITextViews? Is there a way to simply have the elements in the xib 'push' the elements below them down based on their new size?
What is the correct approach to configuring a UITableViewCell with multiple dynamic text views so that my entire application is not comprised of code to calculate heights and coordinates?
Or is the answer that I should not be putting more than one dynamic UITextView in the same UITableViewCell ?
I have used something like this for a cell to calculate its height in
tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
function
CGSize labelSize = [self.comments sizeWithFont:[UIFont fontWithName:#"Verdana" size:17.0]
constrainedToSize:CGSizeMake(280.0f, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 20;
+20 is for padding.
and also if you are having every cell like the one you mention above, than you can create a class of UITableViewCell with following UIView elements inside it.
iOS doesn't seem to have any sort of flow layout engine -- at least none that I can think of.
If, however, your labels are located at the bottom of your UITableViewCell, you might be able to get away with setting the UILabel's autoresizingMask mask property to UIViewAutoresizingFlexibleBottomMargin so that the UILabels will anchor itself at the bottom. This is assuming that you already adjusted the size of the UITableViewCell to fit the entire controls. Otherwise, I think the best way to handle this is to recalculate the y coordinates of the labels.
I found it useful to add this sort of behavior as a category on UIView so a view can resize one of its subviews to fit some arbitrary text, and optionally resize the parent view to fit the newly enlarged subview as needed.
http://blog.carbonfive.com/2009/07/10/resizing-uilabel-to-fit-text/

Adjusting the width of a UILabel in UITableViewCell based on the width of a second UIView in the Cell

I have a UITableViewCell with two subviews, a UILabel on the left, and a random input control on the right. The random input control on the right can vary in size, as can the length of the text, but since I can set the word wrap of the text on the left, I need to be able to adjust the size of the UILabel based on the width of the random input control. To complicate matters, the app needs to work in both portrait and landscape modes, which give the table cells different widths.
This wouldn't be difficult if I could read the width of the table cells and set the widths of its subviews appropriately, but at creation time the width of the cell is 0.
Any ideas?
Nothing easier than that: every UITableViewCell is also a UIView, which has a method designed for just that: layoutSubviews, which is called whenever the view (here: cell) needs a re-layout. This is where you lay out the content.

UITableViewCell strange width?

I have setup a UITableView (320 wide) with a UITableViewCell created in IB, the cell is also 320 wide. After setting a background image (320 wide) in UITableViewCell (or my subclass to be correct) I have noticed that the cells fall short of the right hand side of the UI (notice the blue on the selected cell and the grey on the one above) Does anyone know what is causing this?
Cheers Gary
I was setting the seperator style and the seperator color on the UITableView, this takes 1 pixel off the cell height for the seperator and offsets the backgound by about 5 pixels to the right. If I reduce the background image thats going into the cell from 320x65 to 320x64 it fits perfectly with no offset.
The only difference between the two screens below is:
LEFT: UITableView Seperator = "None" (UITableViewCell background has no offset)
RIGHT: UITableView Seperator = "Single Line" (UITableViewCell background has offset)
DATA:
Cell resolution: 320x65
Cell view: 320x65
Cell Background Image Resolution: 320x65
If you want use the separator your Cell Background Image needs to be 320x64, this will stop it shifting sideways and display the cell correctly.
Cheers Gary
It seems to me that you're just not setting the contentMode of your background image view correctly (i.e. you're setting it to aspectFit or something). Are you sure that it's offset by 5, and not actually shrunk by 5 pixels?
For example, if the original picture is 320x65, and the height is reduced to 64, aspectFit will scale it, while keeping the same aspect ratio, to 64/65 * 320 ~= 315.