Como faço para que a UILabel exiba o texto esboçado?
Tudo que eu quero é uma borda preta de um pixel em torno do meu texto UILabel branco.
Cheguei a subclassificar a UILabel com o código abaixo, que eu desajeitadamente consegui juntar a partir de alguns exemplos on-line tangencialmente relacionados. E funciona, mas é muito, muito lento (exceto no simulador) e eu não consegui centralizar o texto verticalmente (então codifiquei o valor y na última linha temporariamente). Ahhhh!
void ShowStringCentered(CGContextRef gc, float x, float y, const char *str) {
CGContextSetTextDrawingMode(gc, kCGTextInvisible);
CGContextShowTextAtPoint(gc, 0, 0, str, strlen(str));
CGPoint pt = CGContextGetTextPosition(gc);
CGContextSetTextDrawingMode(gc, kCGTextFillStroke);
CGContextShowTextAtPoint(gc, x - pt.x / 2, y, str, strlen(str));
}
- (void)drawRect:(CGRect)rect{
CGContextRef theContext = UIGraphicsGetCurrentContext();
CGRect viewBounds = self.bounds;
CGContextTranslateCTM(theContext, 0, viewBounds.size.height);
CGContextScaleCTM(theContext, 1, -1);
CGContextSelectFont (theContext, "Helvetica", viewBounds.size.height, kCGEncodingMacRoman);
CGContextSetRGBFillColor (theContext, 1, 1, 1, 1);
CGContextSetRGBStrokeColor (theContext, 0, 0, 0, 1);
CGContextSetLineWidth(theContext, 1.0);
ShowStringCentered(theContext, rect.size.width / 2.0, 12, [[self text] cStringUsingEncoding:NSASCIIStringEncoding]);
}
Eu só tenho uma sensação incômoda de que estou negligenciando uma maneira mais simples de fazer isso. Talvez substituindo "drawTextInRect", mas parece que não consigo fazer drawTextInRect se dobrar à minha vontade, apesar de encará-lo com atenção e franzir a testa muito forte.