è possibile applicare sia ictus e riempire con una NSAttributedString
e UILabel
?Come posso sia ictus e riempire con NSAttributedString w/UILabel
33
A
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)
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
Si noti che il "contorno" sarà un contorno interno, non esterno, cioè il contorno si mangerà nell'area di riempimento. – Jason
@Jason c'è un modo per generare un riempimento "esterno" con UILabel + AttributesedText? –
Non ho scoperto come ... – Jason