2013-10-08 12 views
10

Ho una semplice query, ho bisogno di sottolineare UIButton testo e colore. Ho sotto linea testo per Uibutton, tuttavia per quanto riguarda il colore della tinta non funziona. Ho impostato il colore della tinta da xib, non sta prendendo da lì. In basso è il mio codice.Come sottolineare il testo e il colore di uibutton in iOS

NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"Testing"]; 
[commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])]; 
[_helpWithLoginLabel setAttributedTitle:commentString forState:UIControlStateNormal]; 

risposta

8

C'è un attributo separato per textColor (NSForegroundColorAttributeName). Ecco un esempio:

NSArray * keys = [[NSArray alloc] initWithObjects:NSForegroundColorAttributeName, NSUnderlineStyleAttributeName, nil]; 
NSArray * objects = [[NSArray alloc] initWithObjects:self.descriptionTextView.textColor, [NSNumber numberWithInt:NSUnderlineStyleSingle], nil]; 
NSDictionary * linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys]; 
self.descriptionTextView.linkTextAttributes = linkAttributes; 

Spero che questo aiuti ...

+1

chiavi Raggruppamento e gli oggetti in array separati per creare un dizionario non è consigliabile codifica stile perché modifica gli attributi tardi on è soggetto a errori. Un modo più semplice per farlo è la risposta di ChikabuZ in cui definisce la coppia chiave-valore l'una accanto all'altra. – Manuel

4

Sulla base di risposta di Alfie ho architettato questa soluzione:

NSArray * objects = [[NSArray alloc] initWithObjects:self.aButton.titleLabel.textColor, [NSNumber numberWithInt:NSUnderlineStyleSingle], nil]; 
NSArray * keys = [[NSArray alloc] initWithObjects:NSForegroundColorAttributeName, NSUnderlineStyleAttributeName, nil]; 

NSDictionary * linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys]; 

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.aButton.titleLabel.text attributes:linkAttributes]; 

[self.aButton.titleLabel setAttributedText:attributedString]; 
+0

Ho inserito una risposta aggiornata con valori letterali per ottenere una sintassi più breve. – CedricSoubrie

7

In Swift:

let attributes = [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue] 
let attributedText = NSAttributedString(string: button.currentTitle!, attributes: attributes) 

button.titleLabel?.attributedText = attributedText 
5

stessa risposta come @dequin usando i letterali si ottiene una sintassi più breve:

NSDictionary * linkAttributes = @{NSForegroundColorAttributeName:button.titleLabel.textColor, NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)}; 
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:button.titleLabel.text attributes:linkAttributes];    
[button.titleLabel setAttributedText:attributedString]; 
+0

Blocco con iOS7 (XCODE 8.1) – Elad

0

Prova questo: -

extension UIButton { 

    func UnderlineTextButton(title: String?, forState state: UIControlState) 
    { 
     self.setTitle(title, for: .normal) 
     self.setAttributedTitle(self.attributedString(), for: .normal) 
    } 

    private func attributedString() -> NSAttributedString? { 
     let attributes = [ 
      NSFontAttributeName : UIFont.systemFont(ofSize: 12.0), 
      NSForegroundColorAttributeName : UIColor.black, 
      NSUnderlineStyleAttributeName : NSUnderlineStyle.styleSingle.rawValue 
      ] as [String : Any] 
     let attributedString = NSAttributedString(string: self.currentTitle!, attributes: attributes) 
     return attributedString 
    } 
} 

Usi: -

button.UnderlineTextButton(title: "Click Here", forState: 
UIControlState.normal)