UIImage size depending on UITableViewCell height - iphone

I have a UITableView whose cells have dynamic height. What I want to do is basically having an image that works as container for each tableview row like in the following picture. Since the content for each row is different, the image should be resized in height to contain the text of the cell.
I am wondering what is the best practice to achieve it.
Thanks in advance

Have a background image view for the cell and set the frame size based on the content you have to place inside the cell. Then you can use the "stretchableImageWithLeftCapWidth:topCapHeight:" method to create the image that you want to set the cell background.
UIImage *backgroundImg = [[UIImage imageNamed:#"test.png"] stretchableImageWithLeftCapWidth:30 topCapHeight:20];
[cell.bgImageView setFrame:CGRectMake(a, b, c, d];
cell.bgImageView.image = backgroundImg;

Yo can use method sizeToFit for this image. else use the image type as center or scaleToFit.

Related

Lazy loading issue in UITableView

I have implemented lazy loading in my UITableView,
in which, i am downloading images from server, and server contains images with different sizes..
and displays like this,
:
Images getting displayed properly but the problem is UITableViewCell Text is not getting displayed properly align in single line as you can see in the image alignment of UITableViewCell Text is not in the single line
Means:Cell 0 to Cell 4 is displayed properly because server contains images with size of 100*100 for Cell 0 to Cell 4 but alignment of cell 5 and cell 6 is different because server contains image with different size of 85*85
Is there any way in which i can set Text alignment in single line ??
Thanks in advance.
i Suggest you to use Custom table-cell with One image-view and Label and set Imageview contentMode like bellow
imageView.contentMode = UIViewContentModeScaleAspectFill; //will fill the frame
imageView.contentMode = UIViewContentModeScaleAspectFit; //will show the entire image, possibly leaving areas of the frame open.
and your Label set Number of Line 0
I think Custom TableView Cell will do for you.
You can design a cell like below
https://www.dropbox.com/s/r9r6dsmdwmpk50h/customcell.png
You can use
imageView.contentMode = UIViewContentModeScaleAspectFit;
to adjust the imagview width and height based on image size

How to display image and text in a tableviewcell

I have strings like this -
abcdefgh http://abc.abc.jpg adffggjk
The number of images is variable.
How can I display the image and text in a correct sequence in a tabview cell?
This can be achieved by customizing TableViewCell.
Design a data structure in that holds all your data(like a class with String array and image path array). Pass that to your custom cell and there add Lables and ImageView and subview for each string and Image in order you want.
If you have too much of data than you can adjust height of cell in heightForRowAtIndexPath method.
if you have too much of data then designing a UIScrollview and adding them in a Other ScrollView would be better idea.
Hope this is helpful...
Post if you need more help....

Custom UITextField

How would I go about creating a UITextField like the one in this image?
It appears to be slightly larger, specifically in height.
This can be done much better and simpler with a stretchable image:
UIImage *fieldBGImage = [[UIImage imageNamed:#"input.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:20];
[myUITextField setBackground:fieldBGImage];
Think of the background of the text field as split into three sections. A middle section which can be stretched and caps on the ends. Create an image which only needs to be long enough to contain one pixel of this repeating section (a very short text field), and then create a stretchable image from it using stretchableImageWithLeftCapWidth: topCapHeight. Pass the width of the left end cap into 'leftCapWidth.' You can make it stretch vertically as well, but if your background image is the same height as your text box it wont have an effect.
If you're familiar with 9-slice scaling in Flash or Illustrator it's exactly the same concept, except the middle sections are only one pixel wide/tall.
The advantage of this is you don't have to worry about multiple layered objects scaling together and you can resize your text fields any time and the background will stay in tact. It works on other elements too!
You probably want to use the background and borderStyle properties of UITextField. Setting borderStyle as UITextBorderStyleNone and create a custom background image to be stretched and used as the background property would be one approach.
I suggest taking a look at those properties in the UITextField class reference.
You can do this by:
yourTextField.borderStyle = UITextBorderStyleNone;
yourTextField.background = [UIImage imageNamed:#"email-input.png"];
And if you want to give margin to your text inside the textfield, you can do this by:
// Setting Text Field Margins to display the user entered text Properly
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
yourTextField.leftView = paddingView;
yourTextField.leftViewMode = UITextFieldViewModeAlways;
You can also do this through Interface Builder.
just use the following line and it should work
textfield_name.background = [UIImage imageNamed : #"yourImage.png"];
here, "yourImage" is the background image you wanna set...
however, this will work only if your button isnt a roundrect button.So, you can change the type of the button in the Interface Builder or you can use
textfield_name.borderstyle = UITextBorderStyleNone or UITextBorderStyleBezel
and you r gud2go....!
Take an uiimageview set its image property to the image you want as uitextfield background. And on top of this uiimageview place an uitextfield with borderstyle none. This you can do directly in interface builder.

Set row height from the height of an image loaded from a plist

I have a UITableView that displays single large images in each cell. The names of the images are stored in a plist. I would like to adjust the hight of each cell to accommodate the height of the image.
Does anyone know of a way to get the height for an image and use it to set the row height?
I'm having trouble finding this one and really appreciate any help.
The best thing to do would probably be to pre-load the image heights into your plist. Otherwise, you're going to have to load the image (using UIImage's imageWithContentsOfFile: method), and then get its size (a property).
To set custom table row heights, implement -tableView:heightForRowAtIndexPath: in your table's delegate.
The property for the height of a UIImage object - let's call it image - is:
CGFloat imageHeight = image.size.height;
Here is the code that actually worked. I put it under the heightForRowAtIndexPath method.
UIImage *imageForHeight = [UIImage imageWithContentsOfFile:MyImagePath];
imageHeight = CGImageGetHeight(imageForHeight.CGImage);
return imageHeight;
Just using:
CGFloat imageHeight = image.size.height;
as Jason recommended did not work for some reason. But he got me on the right track.

Drop shadows on iPhone Clock App "Alarm" tab

In the clock app that comes with the iPhone there is a tab view for setting alarms. In that view each of the UITableViewCell instances have a drop shadow around them. Does anyone know how to achieve the same effect?
Also it looks like the tab bar at the very bottom has a drop shadow above it as well.
Ideas on how to achieve the same look would be greatly appreciated.
I was wondering how to do that and it just occurred to me to use the UITableView's footer view:
myTableView.tableFooterView = myCustomViewWithDropShadowImage;
I would guess that there is an extra cell which contains just a background image which is the transparent dropshadow. If it's not a cell (because that might create scrolling weirdness) it is probably an extra view positioned below the bottom cell of the UITableview which - again - simply contains an image of the drop shadow.
Like Thomas said, create a 100% width image (say, 320 x 40px on non-retina devices) and create 4 instances of UIImageView with it. The first, at the top of your main view. The second, at the bottom, and in addition do this:
UIImageView* bottomShadow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BottmShadow.png"]]
bottomShadow.transform = CGAffineTransformMakeScale(1, -1);
(Flip it vertically)
Then, do the same with the other two, but place them as subviews of the Table View. One of them just outside the first row:
CGRect tableTopShadowFrame = tableTopShadow.frame;
tableTopShadowFrame.origin.y = -(tableTopShadowFrame.size.height);
[tableTopShadow setFrame:tableTopShadowFrame];
and the other just below the last row (you need to know the height of all the rows together. If your rows are all the same height, it is row height times number of rows).
Finally, you need to set the table's backgroundColor property to transparent
tableView.backgroundColor = [UIColor clearColor];
and possibly set some darkish gray for your main view's background color.