UITableViewCell text alignment and overlapped by subview - iphone

I am working on a todo list application. It's however i cant seems to align my checkbox image properly with the cell.textLabel.text.
My problem for the code below is the words "test test" got cover by my checkbox image, which only left with "st test"
My code for the checkbox button image like
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5.0, 7.0, 25.0, 25.0)];
UIImage *uncheckedButtonImage = [UIImage imageNamed:#"unchecked_checkbox.png"];
[button setImage:uncheckedButtonImage forState:UIControlStateNormal];
[cell.contentView addSubview:button];
and my cell textlabel is just
cell.textLabel.text = #"test test";
Is there anyway the align the textLabel text to move a bit to the right?

I have suggested to Add UILabel because in the future if you needed to change the view cell it will be more Dynamic and easier to play around anything new to the Cell.
take a look to the following code snippet:
CGRect DeviceNameFrame = CGRectMake(101, 32, 522, 21);
UILabel *lblDeviceName = [[UILabel alloc] initWithFrame:DeviceNameFrame];
lblDeviceName.backgroundColor = [UIColor clearColor];
lblDeviceName.font = [UIFont fontWithName:#"Helvetica-Bold" size:17];
lblDeviceName.text = #"test test";
lblDeviceName.lineBreakMode = UILineBreakModeMiddleTruncation;
[cell.contentView addSubview:lblDeviceName];
[lblDeviceName release];
I hope this will help you.

The simplest solution would be to look at the "Indentation" properties of UITableViewCell. See the documentation for more info. Subclassing the cell and adding your own UILabel would give you the most control though.

Related

button action in UITableView Cell

In my app one UITableView is there. In table cell am placing two labels and one button. button height is equal to (label1+ label2) height. these are to display content. and button is for action. Here my problem is am not able to perform button action in all portion. only some part is working when clicking.
Here is the code am used to place the labels and buttons
artistButton = [[UIButton alloc] initWithFrame:CGRectMake(11.0, 6.0, 170.0, 50.0)];
artistButton.backgroundColor = [UIColor redColor];
[self.contentView bringSubviewToFront:artistButton];
[self.contentView addSubview:artistButton];
artistLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(11.0, 6.0, 170.0, 27.0)];
artistLabel1.textAlignment = UITextAlignmentLeft;
artistLabel1.textColor = [UIColor colorWithRed:0.14 green:0.29 blue:0.42 alpha:1];
artistLabel1.font = [UIFont boldSystemFontOfSize:15.0];
artistLabel1.backgroundColor = [UIColor blueColor];
[self.contentView addSubview:artistLabel1];
artistLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(11.0,20.0, 170.0, 27.0)];
artistLabel2.textAlignment = UITextAlignmentLeft;
artistLabel2.textColor = [UIColor grayColor];
artistLabel2.font = [UIFont boldSystemFontOfSize:12.0];
artistLabel2.backgroundColor = [UIColor grayColor];
[self.contentView addSubview:artistLabel2];
any one can help or suggest.
Thanks in advance.
I found answer for my question.
Because of size of the button and labels only it is not working.
Now am adjusted the width and length of button and labels its working fine.
Thank you all.
add one line before UIButton declaration..
artistButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
after that you can initialize your UIButton code.
Add Target action to this Button
artistButton = [[UIButton alloc] initWithFrame:CGRectMake(11.0, 6.0, 170.0, 50.0)];
artistButton.backgroundColor = [UIColor redColor];
[artistButton addTarget:self
action:#selector(ArtistAction)
forControlEvents:UIControlEventTouchUpInSide];
[self.contentView bringSubviewToFront:artistButton];
[self.contentView addSubview:artistButton];
-(void)ArtistAction
{
NSLog("Test Artist Action");
}
I suppose you do realize that the frames you set are overlapping.
You do call [self.contentView bringSubviewToFront:artistButton]; but you add the button as a subview after so it will have no effect.
Also you add other subviews after adding the button and those will be placed above you button.
So just add the two labels first and then the button you should be able to click the button properly, though you labels will be under the button and not sure how much of them will be visible. Check the frames you are setting for the objects you are adding.
Just use for your cell tableView:didSelectRowAtIndex: method.

Set frame of UIButton to specific word in UILabel

I have a problem regarding to set UIButton frame.
NSString *sample = #"Demo to set display view on label in iPhone";
CGSize maximumLabelSize = CGSizeMake(55,9999);
expectedLabelSize = [sample sizeWithFont:[UIFont systemFontOfSize:20]
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
NSLog(#"%f",expectedLabelSize.height);
self.label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, expectedLabelSize.width, expectedLabelSize.height)];
self.label.backgroundColor = [UIColor clearColor];
self.label.numberOfLines = 0;
self.label.text = sample;
[self.label sizeToFit];
So in below uibutton code how to set frame position of text so that only "iPhone" word is clickable?
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.frame = CGRectMake(self.label.frame.origin.x,
self.label.frame.origin.y+50,
expectedLabelSize.width,
expectedLabelSize.height);
To make just one word of a UILabel clickable as a hyperlink you would need to know exactly where the UILabel puts that word. That is difficult (not impossible) especially with a multiple line label and especially if you allow resizing of the font to make the text fit.
Why not use a UIWebView and make it a hyperlink instead? Or separate the text that is clickable and put that text into the UIButton by itself, like:
label.text = #"Demo to set display view on label in ";
button.titleLabel.text = #"iPhone";
Note: you should not checkmark this answer if your problem is not solved.

How to insert more than one line of data in a single cell of UITableView?

Can I know how can we insert more than one line of data into a single cell of a UITableView,
Just Like, I want the output in a single cell to be like :
KPK
Developer
Infosystems
INDIA
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 80)];
myLabel.text = #"KPK";
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.textColor = [UIColor yellowColor];
[cell addSubview:myLabel];
UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 201, 200, 80)];
myLabel1.text = #"Developer";
myLabel1.textAlignment = UITextAlignmentCenter;
myLabel1.textColor = [UIColor yellowColor];
[cell addSubview:myLabel1];
Use \n as you are using in your string.
Set numberOfLines to 4 to allow for any number of lines.
label.numberOfLines = 4;
Update the label frame to match the size of the text using sizeWithFont:. If you don't do this your text will be vertically centered or cut off.
UILabel *label; // set frame to largest size you want
...
CGSize labelSize = [label.text sizeWithFont:label.font
constrainedToSize:label.frame.size
lineBreakMode:label.lineBreakMode];
label.frame = CGRectMake(
label.origin.x, label.orgin.y,
label.size.width, labelSize.height);
label.text=[NSString stringWithFormat:#"KPK\nDeveloper\nInfosystems\nIndia"];
Now you just need to add the label in your cell.
[cell addSubView:label];
you can place \n in text.Not sure if that works else you can make custom cell for multiple line records.
set tablerow height and Use label in your tableview
Create a custom cell, add 4 labels in it, to display all of your required info. Checkout the following tutorial to see how you can make custom cell.
http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/
Use a uilabel and add that one as the subview if your tableview cell.you can specify the number of lines of uilabel
label.numberOfLines = 4;
I think that the best way is creating a custom UITableViewCell implementation, or programmatically adding UILabels to the cell's contentView.
I.e.:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 30)];
label.text = #"MyText";
label.textColor = [UIColor blackColor];
[cell.contentView addSubview:label];
[label release]; // Never forget this!
When adding the additional rows you should update the first line definition to correctly offset the new rows. Eg. the second label should have something like this:
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 35, 200, 30)];
When you are done, you should implement the tableView:heightForRowAtIndexPath: method in the table's delegate, because you have to define the correct height for the new cell. In your example, for 4 labels you should implement something like this:
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath)indexPath {
if (is_one_of_my_modified_cells) {
return 35*4 + 5;
}
return 44; // default height
}
Try it and let me know.

Resize of UIButton Alters the type of UIButton

I am building an app in which I need to resize the button as per the title length. I wrote the following code
`UIButton *newButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
newButton.backgroundColor = [ UIColor clearColor];
newButton.titleLabel.backgroundColor = [ UIColor whiteColor];
[newButton setTitle:#"devsri" forState:UIControlStateNormal];
newButton.titleLabel.textColor = [ UIColor blackColor];
CGSize expectedLabelSize = [newButton.titleLabel.text sizeWithFont:newButton.titleLabel.font];
newButton.frame = CGRectMake(xBase, yBase, expectedLabelSize.width, expectedLabelSize.height);
The above code do resize the table but the button no more remains round rectangle in view. Kindly let me know what is wrong in the above code.
Thanks in advance!!
I got the bug in here. I was initializing the frame with the size of the titlelabel that was eventually bigger than the original button in width. Thus the line
newButton.frame = CGRectMake(xBase, yBase, expectedLabelSize.width, expectedLabelSize.height);
as
newButton.frame = CGRectMake(xBase, yBase, expectedLabelSize.width + 15, expectedLabelSize.height);
This will widen the button enough to accomodate the new size of the label.
:)

How to create a button similar to "Remove Contact" button on iPhone?

Apple has many big colored buttons, that are not standard. One of them is a delete contact button in address book. Another is Start/Stop button in Timer (Clock application) or End Call button in Phone. They are different, but all have similar appearance.
Question is simple. Is there a way to create these kind of buttons without using background images/screenshots or recreating them from scratch?
Thanks.
You could try Opacity. The new release allows you to draw vector images and generate UIKit-friendly Objective-C code that recreates the drawing.
Well, I've tried many ways to do it, but the most simple was to make a UIButton with a custom style.
If you need a "delete" button in the footer of the table - like in contact or event, here is the code:
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UIButton *newDeleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
newDeleteButton.frame = CGRectMake(10, 10, 300, 43);
UIImage *buttonBackground = [UIImage imageNamed:#"ButtonDelete.png"];
[newDeleteButton setImage:buttonBackground forState:UIControlStateNormal];
[newDeleteButton addTarget:self action:#selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UILabel *deleteTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 39)];
deleteTitle.text = #"Delete";
deleteTitle.font = [UIFont boldSystemFontOfSize:20];
deleteTitle.textColor = [UIColor whiteColor];
deleteTitle.textAlignment = UITextAlignmentCenter;
deleteTitle.backgroundColor = [UIColor clearColor];
[footerView addSubview:newDeleteButton];
[footerView addSubview:deleteTitle];
formTableView.tableFooterView = footerView;
First, you create the view for footer.
Then you make a button with the correct background — just make a screenshot of any button you need and delete letters from it.
Than you create a caption for your button and place it over the button.
The last step is to put the button and caption in the footer view, and put the footer view into the table.