2013-04-16 12 views

risposta

71

Sì, la chiave è quello di applicare un valore negativo al NSStrokeWidthAttributeName Se questo valore è positivo si vedrà solo la corsa e non il riempimento.

Objective-C:

self.label.attributedText=[[NSAttributedString alloc] 
initWithString:@"string to both stroke and fill" 
attributes:@{ 
      NSStrokeWidthAttributeName: @-3.0, 
      NSStrokeColorAttributeName:[UIColor yellowColor], 
      NSForegroundColorAttributeName:[UIColor redColor] 
      } 
]; 

Grazie alla @cacau di seguito: Vedi anche Technical Q&A QA1531

versione Swift:

let attributes = [NSStrokeWidthAttributeName: -3.0, 
         NSStrokeColorAttributeName: UIColor.yellowColor(), 
         NSForegroundColorAttributeName: UIColor.redColor()]; 

label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes) 

Swift 3 versione:

let attributes = [NSStrokeWidthAttributeName: -3.0, 
         NSStrokeColorAttributeName: UIColor.yellow, 
         NSForegroundColorAttributeName: UIColor.red] as [String : Any] 

    label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes) 
+0

Si noti che il "contorno" sarà un contorno interno, non esterno, cioè il contorno si mangerà nell'area di riempimento. – Jason

+2

@Jason c'è un modo per generare un riempimento "esterno" con UILabel + AttributesedText? –

+0

Non ho scoperto come ... – Jason

7

Swift versione:

let attributes = [NSStrokeWidthAttributeName: -3.0, 
         NSStrokeColorAttributeName: UIColor.yellowColor(), 
         NSForegroundColorAttributeName: UIColor.redColor()]; 

label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes) 

Swift 3 versione:

let attributes = [NSStrokeWidthAttributeName: -3.0, 
         NSStrokeColorAttributeName: UIColor.yellow, 
         NSForegroundColorAttributeName: UIColor.red] as [String : Any] 

    label.attributedText = NSAttributedString(string: "string to both stroke and fill", attributes: attributes) 
0

Ora Ultimo nella mela rapida rimuovere il [String: Qualsiasi] per la chiave attributi uso dichiarazione di valore come [NSAttributedStringKey: Qualsiasi]

let strokeTextAttributes = [ 
     NSAttributedStringKey.strokeColor : UIColor.green, 
     NSAttributedStringKey.foregroundColor : UIColor.lightGray, 
     NSAttributedStringKey.strokeWidth : -4.0, 
     NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 52) 
     ] as [NSAttributedStringKey : Any] 

    hello_cell_lb.attributedText = NSAttributedString(string: "\(hello_array[indexPath.row])", attributes: strokeTextAttributes) 

grazie