iphone problem making UILabel - iphone

I'm having kind of a dumb problem.
I am using the below code to create/modify a UILabel via code. The reason I am creating it via code is because I need it to be rotated 90 degrees and Im not aware of a way to do that in IB.
What's happening - A user hits a button that makes the text they selected to appear in the UILabel. Then when they select the button again, with different text, the new text appears in place of the old text.
The first time I hit the button, it works perfectly, but the second time I hit the button, the new label appears over the old label and the old label never disappears. I have tried removing the first label, making it nil, just removing the text, but I cannot access any part of the label once it has been created.
ViewController.h
...
UIView *viewForLabels;
UILabel *tab1Label;
}
#property (nonatomic, retain) IBOutlet UIView *viewForLabels;
#property (nonatomic, retain) IBOutlet UILabel *tab1Label
...
#end
ViewController.m
...
#synthesize tab1Label;
...
UILabel *tab1Label = [[UILabel alloc]init];
tab1Label.text = [theText];
tab1Label.backgroundColor = [UIColor clearColor];
tab1Label.textColor = [UIColor blackColor];
tab1Label.opaque = NO;
tab1Label.font = [UIFont systemFontOfSize:14];
tab1Label.numberOfLines = 2;
tab1Label.adjustsFontSizeToFitWidth=YES;
tab1Label.transform = CGAffineTransformMakeRotation (90*3.1459565) / 180);
tab1Label.frame = CGRectMake(2,87,45,119);
[viewForLabels: addSubview: tab1Label];
...

First in your code example you alloc tabLabel1 and then run a bunch of property updates against a different named object tab1Label.
Sorry if I am misunderstanding the question but why are you creating a second label? Per this part of your description:
What's happening - A user hits a
button that makes the text they
selected to appear in the UILabel.
Then when they select the button
again, with different text, the new
text appears in place of the old text.
Just update the .text property and any sizing needed why use a whole separate object?

Related

How do I use a button to toggle between highlighted in non-highlighted images in a UIImageView object?

I am trying to use one button to toggle between two images in a UIImageView object. I've been searching around for a problem/answer close to my specific problem but have had no luck, so here goes...
MainViewController.h
IBOutlet UIButton *selectionButton;
UIImageView *selectionStatus;
#property(nonatomic, retain) UIButton *selectionButton;
#property(nonatomic, retain) UIImageView *selectionStatus;
- (IBAction)selectionButtonPressed: (id)sender;
MainViewController.m
#synthesize selectionButton;
#synthesize selectionStatus;
- (IBAction)selectionButtonPressed: (id)sender
{
if (selectionStatus.highlighted == YES)
selectionStatus.highlighted = NO;
else
selectionStatus.highlighted = YES;
}
-(void)viewWillAppear:(BOOL)animated {
// selection images
selectionStatus = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"NotSelected.png"]
highlightedImage:[UIImage imageNamed:#"Selected.png"]];
}
When I run this in the simulator, the images don't appear in "SelectionStatus", so I think I'm missing something big. Help would be very much appreciated. Thanks!
I should have said more about the button. selectionButton is a custom button that overlays the selectionStatus imageview as well as a label that displays the results of a calculation. Tapping selectionButton selects the value in the label for inclusion in a calculation of an average of up to three selected label values. The image displayed in selectionStatus is a checkmark that changes colors depending on the selection status. I've found another approach that works, but doesn't seem as elegant:
- (IBAction)selectionButtonPressed: (id)sender
{
if (toggleValue == YES) {
toggleValue = NO;
UIImage *unselect = [UIImage imageNamed:#"NotSelected.png"];
selectionStatus.image = unselect;
}
else {
toggleValue = YES;
UIImage *select = [UIImage imageNamed:#"Selected.png"];
selectionStatus.image = select;
}
}
This approach does not require the use of viewWillAppear, and initialization is done in viewDidLoad. Thanks for your comments and suggestions.
In ib , set the button's background image property.I think your image is very big,in fact it is show on your button when you set your button's state into highlight.
Just try,I am not sure.

I am making a game, and I want a number to be added to a label when the user taps a button

I am making a game, and I want a number to be added to a label when the user taps a button. Like it adds 5 points when the button.
Declare IBOutlet UILabel *scoreLabel; and implement this method:
-(IBAction)buttonPressed {
scoreLabel.text = [NSString stringWithFormat:#"%d",[scoreLabel.text intValue]+5];
}
Make sure you set up the connections in the IB (scoreLabel points to the label and the button triggers the action.
In Cocos2d you can create labels that can have a numerical value attributed to them.
The pseudocode for this question would be as such:
int points = 5;
UIButton *pushButton;
UILabel *Label;
-(void)OnButtonPress{
points =+ 5 //add 5 points
}
label.text = [NSString stringWithInt:points];
For a tutorial on Cocos2d labels, please see:
http://www.usightread.com/2009/06/labels-menus-and-options-in-cocos2d-iphone/

UIView on iPad with GroupTableViewBackgroundColor

I have a UIView that I have dropped inside of another UIView in IB (iPad version), I am doing this so I can control the background color of the region.
Odd thing is that if I set the background color of the UIView to GroupTableViewBackgroundColor, either in IB or code, the color is always white, it does not respect the color change, nor is it reflected in IB.
Has anyone seen this behavior and found a fix?
Thanks in advance.
I think I know what you are saying. You want the view to be the same as a group table view even though it isn't a group table view. This is what I did:
UITableView *tv = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
UIView *backgroundView = tv.backgroundView;
[self.view addSubview:backgroundView];
[tv release];
CGRect backgroundFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[backgroundView setFrame:backgroundFrame];
[self.view sendSubviewToBack:backgroundView];
I am not sure if it is related to your problem but Apple introduced some changes to UITableView for iOS 3.2. They introduced a new property to UITableView defined as follows:
#property (nonatomic, readwrite, retain) UIView *backgroundView;
This new view sits behind the table cells, header and footer which can be confusing if you set the table view background color. As a quick workaround, if you just want to change the color, you can remove the new view:
tableView.backgroundView = nil;
You should then get the same behavior as on the iPhone when you set tableView.backgroundColor. (Of course this may all change with iOS 4.2).

Show/Hide iphone UI elements based on prefs - how to?

I've got a simple form in my iPhone app. The form is laid out and managed via IB and the typical wiring (i.e. I am not creating this form programmatically).
One of the fields (and its associated label) should be shown only if a particular preference is set.
I could set the field and label's alpha to 0 and disable them in this case. The problem is that the fields below this now-invisible field would remain in the same place and there would be a big blank area. My goal is to have the screen look normal in either state.
Is there a way to programmatically remove (or add) UI elements and have those below shift up or down to make room? Or should I consider making a whole other NIB file for this second case? (and, if I do that, is there an easy way to share the common elements?)
Current UI with both controls shown
With Both http://img.skitch.com/20100704-bm41w6wtqkdgh1da99ihb7g32d.jpg
UI with optional control hidden via alpha == 0
Using Alpha to Hide http://img.skitch.com/20100704-q2sxrj3nf6ya68wp6ubn86n2pa.jpg
Desired UI with optional control hidden
Desired when hidden http://img.skitch.com/20100704-82r876pgctee8gb51ujg1dwj7k.jpg
When every UI element is linked to a IBOutlet pointer, e.g.
#property (nonatomic, retain) IBOutlet UITextField *field_a;
#property (nonatomic, retain) IBOutlet UITextField *field_b;
#property (nonatomic, retain) IBOutlet UITextField *field_c;
// ...
You can test each element's visibility by:
if (field_a.hidden) {
// ...
} else {
// ...
}
And move them around:
CGPoint pt = field_a.center;
pt.y -= 60;
field_a.center = pt;
Or by some animation:
CGPoint position = field_a.center;
position.y -= 60;
[UIView beginAnimations:#"MoveUp" context:NULL];
[UIView setAnimationDuration:0.5];
field_a.center = position;
[UIView commitAnimations];
To hide an element:
field_a.hidden = YES;
To show an element:
field_a.hidden = NO;
Use the cocoa touch properties:
.hidden 1
.userInteractionEnabled 0
Or you could:
.alpha = 0
I've seen a tutorial about this recently that involved moving a subview further down in the main view when a segmented control was selected. I believe it was an animation triggered by a beginAnimations:context:, but I can't find a reference to that tutorial right now.
Essentially, there were views under a view that were hidden, and one set was moved out of the way and the other controls unhidden.

Changing Size of Text on a UIButton

Client wants me to do something, i have just checked and i m quite sure its not possible.
He asked to put a text on a Button ( UIButton ). (Default State Configuration)
And when the user clicks it the text should enlarge. (Highligted State Configuration)
I have checked by selecting Highligted State Configuration and then changed the size of text in the Interface Builder but i didnt work . The text changed for Default State Configuration as well. Please tell me if there is any way to do it.
Thanks!
Taimur
You might not be able to do it in IB, but you can certainly do it with a pair of actions. I'm assuming that the highlighted state will only be active while the button is pressed. You can modify this code if that's not the case (to toggle the state back to normal when the highlighted state ends).
- (IBAction)buttonDown:(id)sender {
UIButton *button = (UIButton *)sender;
button.titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
}
- (IBAction)buttonUp:(id)sender {
UIButton *button = (UIButton *)sender;
button.titleLabel.font = [UIFont boldSystemFontOfSize:15.0];
}
btn.titleLabel.adjustsFontSizeToFitWidth = TRUE;
so It adjust text size as per button width automatically......
This actually isn't too hard to do. You need to create two IBActions in your class, onTouchDown and onTouchUp. Create an IBOutlet for your button too. Then, go to Interface Builder and wire up the button to the IBOutlet and wire up the action for "Touch Down" to "onTouchDown" and the actions for "Touch Up Inside" and "Touch Up Outside" to "onTouchUp."
onTouchDown is where you'll set your highlighted font. onTouchUp is where you'll reset your button.
Your header file will end up looking something like this:
#interface TestViewController : UIViewController {
UIButton *testButton;
}
#property (nonatomic, retain) IBOutlet UIButton *testButton;
- (IBAction)onTouchDown;
- (IBAction)onTouchUp;
Now, inside the "onTouchDown" function here's what you're gonna do:
1) Store the current dimensions of the button
2) Change the font size
3) Tell the button to auto-resize (THIS IS THE KEY)
4) Center the button based on the old size
You should have a function that ends up looking something like this:
- (IBAction)onTouchDown:(id)sender
{
CGRect oldBtnRect = testButton.frame;
testButton.titleLabel.font = [UIFont systemFontOfSize:36];
[testButton sizeToFit];
testButton.frame = CGRectMake(testButton.frame.origin.x - ((testButton.frame.size.width - oldBtnRect.size.width)/2), testButton.frame.origin.y - ((testButton.frame.size.height - oldBtnRect.size.height)/2), testButton.frame.size.width, testButton.frame.size.height);
}
Note, the font size is 36.
In your touch up function, it'll look something like this:
- (IBAction)onTouchUp:(id)sender
{
CGRect oldBtnRect = testButton.frame;
testButton.titleLabel.font = [UIFont systemFontOfSize:15];
[testButton sizeToFit];
testButton.frame = CGRectMake(testButton.frame.origin.x - ((testButton.frame.size.width - oldBtnRect.size.width)/2), testButton.frame.origin.y - ((testButton.frame.size.height - oldBtnRect.size.height)/2), testButton.frame.size.width, testButton.frame.size.height);
}
The font size should be whatever your default font is. In this case, I made it 15.
This should get you a button that resizes from the center instead of just resizing.
Now, the code isn't intended to be perfect. It's just a general IDEA of what you're doing here. I think this is a great candidate for some code reuse by combining these into one function and passing in what size you want the text to be. I just didn't feel like doing it myself ;)