Moving labels on circle - iphone

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"];

Related

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.

how to change the size of UILabel

I have a UILabel coded programmatically. I want to change the size of the label when i pressed a button. how to change the size of that label? this is my code
UILabel *theLabel11 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,30)];
[theLabel11 setText:#"US"];
[theLabel11 setTextAlignment:UITextAlignmentCenter];
[theLabel11 setFont: [UIFont fontWithName:#"Arial" size:13.0f]];
[theLabel11 setBackgroundColor:[UIColor orangeColor]];
[theLabel11 setTextColor:[UIColor blackColor]];
[scroll1 addSubview:theLabel11];
You should declare your label as class property, so it can be accessed from other methods
To change the font size use
[theLabel11 setFont: [UIFont fontWithName:#"Arial" size:13.0f]];
To change the frame size of the label us
theLabel11.frame = CGRectMake(x, y, width, height);
A common idiom for adjusting the spatial information on a UIView is as below
label.frame = CGRectMake(
x,
y,
width,
height
);
You can get the old position and height via
label.frame.origin.x
label.frame.origin.y
label.frame.size.width
label.frame.size.height
If there is only one label added to scroll1 then, iterate the scrollview to get the label reference as follows in button action
for(UIView *subView in scroll1.subViews){
if([subView isKindOfClass:[UILabel class]]){
UILabel *lbl=(UILabel*)subView;
//change size of label here
}
}
if there are many labels assign a tag to each label while creating and check that in for loop

SetCenter problem for an UILabel

OK, I'm struggling to make this work but without success.
Basically I want to add a UILabel to an UIView and center it.
The code looks like this:
UIView *customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[customTitleView setBackgroundColor:[UIColor clearColor]];
// Screen title
CGSize constraint = CGSizeMake(200.0, 44.0f);
CGSize size = [screenTitle sizeWithFont:[UIFont boldSystemFontOfSize:20.0]
constrainedToSize:constraint
lineBreakMode:UILineBreakModeCharacterWrap];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, size.width, size.height)];
[titleLabel setText:screenTitle];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20.0]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
titleLabel.center = customTitleView.center;
[customTitleView addSubview:titleLabel];
[titleLabel release];
self.navigationItem.titleView = customTitleView;
[customTitleView release];
I expected that the UILabel would be centered within the UIView.
Well, it isn't. It is somehow right aligned and not even close to the center of the UIView.
What am I doing wrong?
So here's what's going wrong:
self.navigationItem.titleView = customTitleView;
When you set customTitleView as the title view of the navigation item the frame of customTitleView will change. customTitleView won't be 320 pixels wide anymore, but the frame of titleLabel will remain the same. Thus it will no longer be centered.
Can't you just set titleLabel as titleView? I think it should be centered automatically. If not, I know a more complicated way to solve it. Drop a comment and I'll tell you how.
EDIT: So here is how you realign the titleLabel after customTitleView's frame has changed.
When the frame of a view is changed layoutSubviews is called on that view. So instead of letting customTitleView be a regular UIView you need to make it a custom view that inherits from UIView. In that custom view you override layoutSubviews and in your implementation of layoutSubviews you make sure everything is aligned the way you want based on the new frame (self.frame). I'll leave the implementation to you.

UILabel Setting Transparent Background Color?

I am looking to add a black label with a transparent background to my view (see below)
// ADD LABEL
UILabel *label = [[UILabel alloc] init];
[label setFrame:CGRectMake(124, 312, 72, 35)];
[label setText:#"Yay!"];
[label setTextAlignment:UITextAlignmentCenter];
[[self view] addSubview:label];
[label release];
When I add the label it always comes out black text on a white background. I have looked in the NIB and the only way I can see to make this work is set the background > color > opacity to 0, or in Xcode:
[label setBackgroundColor:[UIColor clearColor]];
Is this the right way to do this?
Cheers Gary
I can confirm that creating a label programatically with background color set using
[label setBackgroundColor:[UIColor clearColor]];
will work. I'm using this in one of my apps.
Also, when you set the background color in Interface Builder, you can set the opacity of the background color to 0. Make sure that you don't set the opacity of the label itself to 0, or the text will also be transparent.
Swift 3 and 4:
yourView.backgroundColor = UIColor.clear

moving label to centre of cell

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.