2012-12-13 6 views
0

J'utilise le code ci-dessous pour définir la couleur du texte dans mon contrôle segmenté. Cependant, cela ne semble pas fonctionner. Est-ce que je rate quelque chose ici?Impossible de changer la couleur de la police dans segmentedControl

// Set up segment control 
NSArray *itemArray = [NSArray arrayWithObjects: @"Popular", @"Starred", nil]; 
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray]; 
segmentedControl.frame = CGRectMake(40, 200, 220, 20); 
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 
segmentedControl.selectedSegmentIndex = 0; 

self.navigationItem.titleView = segmentedControl; 

for (id segment in [segmentedControl subviews]) 
{ 
    for (id label in [segment subviews]) 
    { 
     if ([label isKindOfClass:[UILabel class]]) 
     { 
      [label setTextAlignment:UITextAlignmentCenter]; 
      [label setColor:[UIColor blackColor]]; 
     } 
    }   
} 

enter image description here

Répondre

0

Si vous ciblez iOS 5+ alors vous devriez le faire comme ceci:

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
         [UIFont boldSystemFontOfSize:17], UITextAttributeFont, 
         [UIColor blackColor], UITextAttributeTextColor, 
         nil]; 
[_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; 
NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]; 
[_segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted]; 
Questions connexes