0

我的自定义UISegmented控件具有[每个州的背景图像]。 如何在图片上方添​​加文字[对于每种状态,这意味着当索引0将被选中时,我将拥有特定的颜色和字体]?将背景图像上方的文字添加到uiswitch

这里是我的UISegmented控制看起来像:

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

和checkOnOffState方法是这样的:

-(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]; 
} 
} 

回答

1

我不知道我完全理解你以后,却苦于您只需使用UILabels(位于图像上方)。然后,可以使用myLabel.text和myLabel.textColor轻松更改颜色和文本。

希望这有助于某种方式!

+0

@Job W 09:我已经尝试使用UILabel将文本添加为​​子视图,但将其置于图像下方... –