Another custom UISwitch question - iphone

I have looked all over stack overflow, for the answer to this question, and i can only find ways which worked on 4.1 and below, but for 4.2.1 and above, i can't find a new way of getting the text of ON and OFF to change. Is there a way of changing it to say Yes and No.
This is what I have currently:
UISwitch *switchControl = [[UISwitch alloc] initWithFrame:frame];
[switchControl addTarget:self action:action forControlEvents:UIControlEventValueChanged];
switchControl.backgroundColor = [UIColor clearColor];
switchControl.on = value;
[cell addSubview:switchControl];
[switchControl release];
What would be the easiest way of changing the text of the UISwitch? Or would it be easier if i was to make two images and using animation to make them flow, like it is a normal UISwitch.

I wrote up a custom switch class. Hope this helps. http://cl.ly/4OQN
EDIT:
Also, maybe try this:
switchView = [[UICustomSwitch alloc] initWithFrame:CGRectZero];
[switchView setCenter:CGPointMake(160.0f,260.0f)];
[switchView setLeftLabelText: #"Foo"];
[switchView setRightLabelText: #"Bar"];
[[switchView rightLabel] setFont:[UIFont fontWithName:#"Georgia" size:16.0f]];
[[switchView leftLabel] setFont:[UIFont fontWithName:#"Georgia" size:16.0f]];
[[switchView leftLabel] setTextColor:[UIColor yellowColor]];

Related

UITextField placeholder shifts when subclassed

I am using a subclassed UITextField to use a custom font. When I set a placeholder for that textfield it gets shifted up a little, but the when I start entering the text, the frame of text has no problem. Is there a way to fix the placeholder shifting issue.
self.txtFirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.txtFirstName.autocapitalizationType = UITextAutocapitalizationTypeSentences;
self.txtFirstName.placeholder = #"Enter First Name here";
self.txtFirstName.textAlignment = UITextAlignmentLeft;
I had same issue and I tried setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter which solved my issue.
UITextField *txtPassword = [[UITextField alloc] initWithFrame:CGRectMake(15,55, 255, 25)];
[txtPassword setPlaceholder:#"Password..."];
[txtPassword setBackgroundColor:[UIColor whiteColor]];
[txtPassword setFont:[UIFont fontWithName:#"Helvetica" size:15]];
[txtPassword setSecureTextEntry:YES];
[txtPassword setTextAlignment:NSTextAlignmentCenter];
[txtPassword setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[txtPassword becomeFirstResponder];
Hope this helps.

how to bold particular word in label in iphone

What me actually doing:-
lbl1= [[UILabel alloc] init];
lbl1.frame=CGRectMake(10, 10,90, 40);
lbl1.textColor=[UIColor blackColor];
lbl1.text=#"Definitions";
lbl1.font = [UIFont boldSystemFontOfSize:16.0f];
UILabel* finalLabel=[[UILabel alloc] init];
finalLabel.frame=CGRectMake(100, 10,90, 40);
finalLabel.text=[NSString stringWithFormat:#"%#:,The following terms shall have the described meaning:",lbl1];
finalLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
finalLabel.numberOfLines=2;
[self.view addSubview:lbl1];
[self.view addSubview:finalLabel];
Didn't get the proper result of final label
it print:- Definitions
UILABEL:0X68640;frame(10,10,90,40);text='Definition',CliptoBound=YES;Userintraction......
You mean partially bold in a single UILabel. right?
You should check this ONE
Check out with below urls:
Custom Label 1
Custom Label 2
It will be helpful to you and your functionality can be achieved.
Cheers!

Reference counted object is used after being it is released

I am getting memory warning as "Reference counted object is used after being it is released"
For this i have tried autorelease also. but no use.
below is the code which gives my warning.what is the issue. pls let me know
UICustomSwitch *switchView = [[UICustomSwitch alloc] initWithFrame:CGRectZero];
[switchView initWithFrame:CGRectMake(180,5,260,30)];
[switchView setOn:YES];
mySearchType = 1;
[[switchView rightLabel] setFont:[UIFont fontWithName:#"Georgia" size:16.0f]];
[[switchView leftLabel] setFont:[UIFont fontWithName:#"Georgia" size:16.0f]];
[switchView addTarget:self action:#selector(switchToggled:)forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:switchView];
change the following code
UICustomSwitch *switchView = [[UICustomSwitch alloc] initWithFrame:CGRectZero];
[switchView initWithFrame:CGRectMake(180,5,260,30)];
into
UICustomSwitch *switchView = [[UICustomSwitch alloc] initWithFrame:CGRectMake(180,5,260,30)];

UIBarButtonItem Title Change

I have the following UIBarButton Item:
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 100.0f, 45.0f, 30.0f)];
[backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchDown];
backButton.titleLabel.text = #"Back";
backButton.titleLabel.font = [UIFont fontWithName:#"texgyreadventor-regular.otf" size:20.0f];
backButton.titleLabel.textColor = [UIColor whiteColor];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
_backItem = backItem;
In some other method, I set it to be on the navigation bar as follows:
[self.navigationItem setLeftBarButtonItem:_backItem];
So far, everything works as expected. But right before I add it on the navigation bar on top, I would like to change its text. Here's one the things I tried doing:
self.navigationItem.leftBarButtonItem.title = #"NEW TITLE";
This however, does not work. How can I add a title during runtime on the fly?
You can access the button again using:
UIView* view = self.navigationItem.leftBarButtonItem.customView;
UIButton* button = (UIButton*)view;
[button setTitle: #"NEW TITLE" forState: UIControlStateNormal];
Are you starting iOS Nayefc, I see you have a lot of questions these last few days :)
For anyone looking (like me just now), in newer versions of iOS the accepted answer no longer works. It has become barButton.title, which is a read-write String? (in Swift) or NSString(in Obj-C).
Instead of
self.navigationItem.leftBarButtonItem.title = #"NEW TITLE";
use
UIButton *backButton = (UIButton *)self.navigationItem.leftBarButtonItem.customView;
backButton.titleLabel.text = #"changed";
UPDATE:
If you want to change the title before adding it on the nav bar do the following
UIButton *backButton = (UIButton *)_backItem.customView;
backButton.titleLabel.text = #"changed";
Here's the fix:
[button setTitle:#"Back" forState:UIControlStateNormal];
The state has to be defined.

After adding a UILabel to my view, how do I delete it?

I have added a UILabel to my view programmatically like this:
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 30.0f)];
myLabel.center = CGPointMake(160.0f, 120.0f);
myLabel.backgroundColor = [UIColor clearColor];
myLabel.textColor = [UIColor whiteColor];
myLabel.font = [UIFont fontWithName:#"Helvetica" size: 18.0];
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.text = #"Hello";
[self.myView addSubview:myLabel];
To add a label to my view. What I can't seem to find out is once I'm done w/ the label (at a future point) how can I delete it from the view? [myLabel release] doesn't seem to work which I think makes sense because the view it's added to probably retained it's over reference. So what is the best practice?
[myLabel removeFromSuperview];