2015-02-13 18 views
6

Ho usato il seguente codice per impostare lo sfondo di UISegmentedControl.Come impostare il colore della tinta del solo testo del segmento selezionato usando Swift?

homeSegment.setDividerImage(UIImage(named: "seperator.png"), forLeftSegmentState: UIControlState.Normal, rightSegmentState: UIControlState.Normal, barMetrics: UIBarMetrics.Default) 



homeSegment.setBackgroundImage(UIImage(named: "squareSegment.png"), forState: UIControlState.Selected, barMetrics: UIBarMetrics.Default) 
homeSegment.setBackgroundImage(UIImage(named: "squareSegment.png"), forState: UIControlState.Normal, barMetrics: UIBarMetrics.Default) 

Come è possibile impostare il colore del testo del segmento selezionato?

risposta

7

L'ho ottenuto utilizzando setTitleTextAttributes. L'esempio seguente utilizza un dizionario per impostare gli attributi poiché è molto probabile che si desideri impostare il tipo e la dimensione del font contemporaneamente. Provate a impostare il colore del testo segmento selezionato al rosso:

let segAttributes: NSDictionary = [ 
     NSForegroundColorAttributeName: UIColor.redColor(), 
        NSFontAttributeName: UIFont(name: "Avenir-MediumOblique", size: 20)! 
] 

homeSegment.setTitleTextAttributes(segAttributes as [NSObject : AnyObject], forState: UIControlState.Selected) 

Esempio:

enter image description here

0

@Portland Runner risposta è per la versione precedente di Swift.

Swift 3.0, questo funziona.

logica è uguale alla @Portland Runner, ma la sintassi vengono modificate e aggiornate in base alle Swift 3.0

qui Questo codice ho implementato è l'estensione per il controllo del segmento e può essere utilizzato per tutti i controlli del segmento nell'applicazione, in cui il set di codice deve essere definito nella classe dell'applicazione.

extension UISegmentedControl { 
func setSegmentStyle() { 
    setBackgroundImage(imageWithColor(color: backgroundColor!), for: .normal, barMetrics: .default) 
    setBackgroundImage(imageWithColor(color: tintColor!), for: .selected, barMetrics: .default) 
    setDividerImage(imageWithColor(color: UIColor.clear), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default) 



    let segAttributes: NSDictionary = [ 
      NSForegroundColorAttributeName: UIColor.gray, 
      NSFontAttributeName: UIFont(name: "System-System", size: 14)! 
     ] 

     setTitleTextAttributes(segAttributes as [NSObject : AnyObject], for: UIControlState.selected) 
    } 

    // create a 1x1 image with this color 
    private func imageWithColor(color: UIColor) -> UIImage { 
     let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0) 
     UIGraphicsBeginImageContext(rect.size) 
     let context = UIGraphicsGetCurrentContext() 
     context!.setFillColor(color.cgColor); 
     context!.fill(rect); 
     let image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     return image! 
    } 
} 

Ovunque per i segmenti il ​​codice qui sotto può essere utilizzato

self.mySegment.setSegmentStyle() 

enter image description here

3

per Swift 4,0

let fontAttribute = [NSAttributedStringKey.font: UIFont(name: "Montserrat-Regular", size: 12)!, 
         NSAttributedStringKey.foregroundColor: UIColor.red] 

segmentedControl.setTitleTextAttributes(fontAttribute, for: .normal) 
Problemi correlati