moving label to centre of cell - iphone

i have created my label using this
UILabel *label1 = (UILabel *) [cell viewWithTag:1];
i want to move my label to centre of the cell...
can any one answer it....
thank you for valuable answers...

The cell's center point is in the cell.superview's coordinates, just convert that point to the cell's coordinates and set the label's center there:
label1.center = [cell.superview convertPoint:cell.center toView:cell];

just set the labels frame to the appropriate offset :
x=cell.frame.width/2-label1.frame.width/2;
y=cell.frame.height/2-label1.frame.height/2;
label1.frame=CGRectMake(x,y,cell.frame.width,cell.frame.height);

Below code adds a label to the center of a collection view cell.
// self is the current cell.
UILabel *pointLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
pointLabel.textAlignment = NSTextAlignmentCenter;
pointLabel.textColor = [UIColor yellowColor];
pointLabel.text = #"+300";
[self addSubview:pointLabel];
Hope this helps.

Related

Moving labels on circle

I have a circle that turns over, how can i put on him some labels that will turn with him also?
my point is that the circle and labels should be on same angel.
Make the label a subview of the UIView you are using to present the circle.
In IB just drag the label onto the the UIView you are using for the circle image or if you are not using IB just add the label to the circle like this:
[circleUIView addSubview:theUILabel];
Download demo source code this url http://www.devedup.com/index.php/2010/03/03/iphone-animate-an-object-along-a-path/
Now changes into Canvas.m file's animateCicleAlongPath method.
Replace to image to label.
UILabel *label = [[UILabel alloc]init];
[label setText:#"test"];
[label setTextColor:[UIColor redColor]];
//Set the frame of the view - which is used to position it when we add it to our current UIView
label.frame = CGRectMake(1, 1, 20, 20);
label.backgroundColor = [UIColor clearColor];
[self addSubview:label];
[label.layer addAnimation:pathAnimation forKey:#"moveTheSquare"];

Placing an image in the bottom of an UITextfield as background

After a lot of search I am posting this Question, I have to place an image at the bottom of a UITextfield, I have tried it with the following code :
letterField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
letterField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
letterField.textAlignment = UITextAlignmentCenter;
letterField.borderStyle = UITextBorderStyleNone;
UIImageView *myView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"text_line.png"]];
[letterField setLeftView:myView];
[letterField setRightViewMode:UITextFieldViewModeAlways];
[myView release];
// letterField.background = [UIImage imageNamed:#"text_line.png"];
// [letterField setBackground:[UIImage imageNamed:#"text_line.png"]];
letterField.textColor = [UIColor whiteColor];
letterField.returnKeyType = UIReturnKeyDone;
[yourtextfield insertSubview:imgViewTitleBackGround atIndex:0];// mgViewTitleBackGround is image view with background image.
//Or
[yourtextfield setBackground:[UIImage imageNamed:#"text_line.png"]];// you can add your image
yourtextfield.textColor = [UIColor redColor];
Hope, this will help you..enjoy
Sorry, my previous answer was totally wrong. From what I can see your image is text_line.png - i suppose it's some kind of separator line, what can i suggest you to try is create a new UIView with the bounds of the UITextField, then add the text field inside this view and add the UIImage view again as subview of this view centering it properly. I don't know if this will fit your needs, but you can give it a try.

How to put an image and write text in same UILabel?

I want to put an image in my UILabel which should be right aligned and write some text in the same UILabel which should be left aligned. How can i do that programatically? Thanks in advance
Set that image as background image
theLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"blah"]];
and write text as
thelabel.text = #"abc"
set the align of the label
[thelabel setTextAlignment:UITextAlignmentLeft];
I am not sure that you can actually put an image inside a UILabel, but if you can, you'll be using the addSubView: method.
UILabel *label = ....
label.text = #"Hi there";
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,width,height)];
[label addSubView:image];
Try it out and see if it works, if not, then you'll probably have to set the image's frame to a frame that floats along with the label's.

UIScrollView Inside UITableViewCell

I'm trying to add a UIScrollView inside my TableViewCell. It appears to be going well, however when the label comes in from the right as I'm scrolling, it comes in above the cell, but as the text moves to the left edge of the label, it disappears behind the label like I want it to.
Any suggestions?
UIScrollView *previewScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width, 80.0)];
previewScrollView.backgroundColor = [UIColor clearColor];
[previewScrollView setContentSize:CGSizeMake(500, 60.0)];
previewScrollView.showsHorizontalScrollIndicator = YES;
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15.0, 3.0, 500.0, 20.0)];
headerLabel.font = [UIFont boldSystemFontOfSize:14.0];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.text = #"My long text string that I hope work out ok";
[previewScrollView addSubview:headerLabel];
[[cell contentView] addSubview:previewScrollView];
//[cell addSubview:previewScrollView];
[previewScrollView release];
from the right as I'm scrolling, it comes in above the cell
Above the cell as in y or z axis?
If it is above in the y axis, you could try setting clipsToBounds on the scroll view. That makes it so subviews are only drawn within the area of their superview.
If it is above in the z axis, where something in front should be behind, try using:
[[cell contentView] insertSubview:previewScrollView atIndex:0];
inserting at index 0 ensures a view is behind all other subviews in the superview.

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.