我想要的只是白色UILabel文本周围有一个单像素的黑色边框.
我甚至用下面的代码将UILabel子类化,这些代码是我从几个无关的在线示例中笨拙地拼凑而成的.它可以工作,但是非常、非常慢(除了在模拟器上),我也不能让它垂直居中文本(所以我暂时在最后一行硬编码了y值).啊哈!
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]);
}
我只是有一种喋喋不休的感觉,觉得我忽略了一种更简单的方法来做这件事.也许是通过重写"drawTextInRect",但是我似乎根本不能让draTextInRect屈从于我的意愿,尽管我全神贯注地盯着它,皱着眉头,真的真的很难.