简体   繁体   中英

Adding text above background image to uiswitch

My custom UISegmented control has background images [for each state]. How do I add text above the images [for each state, meaning that when index 0 will be selected I will have a specific color and font] ?

Here's how my UISegmented control looks like:

UISegmentedControl *customUISC=[[UISegmentedControl alloc] initWithItems:[[[NSMutableArray alloc] initWithObjects:@"ON",@"OFF",nil] autorelease]];

    [customUISC setFrame:CGRectMake(11,14,298,28)];
    customUISC.selectedSegmentIndex=0;
    customUISC.segmentedControlStyle=UISegmentedControlStyleBar;

    [customUISC setImage:[UIImage imageNamed:@"FirstTabActive.png"] forSegmentAtIndex:0];
    [customUISC setImage:[UIImage imageNamed:@"SecondTab.png"] forSegmentAtIndex:1];
    [customUISC addTarget:self action:@selector(checkOnOffState:) forControlEvents:UIControlEventValueChanged];

and the checkOnOffState method looks like:

-(IBAction)checkOnOffState:(id)sender{

UISegmentedControl *iSeg=(UISegmentedControl *)sender;

if(iSeg.selectedSegmentIndex==0){

    [iSeg setImage:[UIImage imageNamed:@"FirstTabActive.png"] forSegmentAtIndex:0];
    [iSeg setImage:[UIImage imageNamed:@"SecondTab.png"] forSegmentAtIndex:1];
}
else {

    [iSeg setImage:[UIImage imageNamed:@"FirstTab.png"] forSegmentAtIndex:0];
    [iSeg setImage:[UIImage imageNamed:@"SecondTabActive.png"] forSegmentAtIndex:1];
}   
}

I'm not sure I fully understand what you're after, but could you just make use of UILabels (positioned above the images). You could then change the colours and text quite easily using myLabel.text and myLabel.textColor.

Hope this helps in some way!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM