【发布时间】:2010-11-09 08:14:23
【问题描述】:
我想要的只是在我的白色 UILabel 文本周围有一个 1 像素的黑色边框。
我用下面的代码对 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”,但我似乎无法让 drawTextInRect 屈服于我的意愿,尽管我专心地盯着它,皱着眉头。
【问题讨论】:
-
澄清 - 我的应用程序中的缓慢很明显,因为当标签的值随着轻微的增长和收缩而变化时,我正在为标签设置动画。没有子类化它很流畅,但是标签动画上面的代码很不稳定。我应该只使用 UIWebView 吗?我觉得这样做很愚蠢,因为标签只显示一个数字......
-
好吧,看起来我遇到的性能问题与大纲代码无关,但我似乎仍然无法让它垂直对齐。由于某种原因,pt.y 总是为零。
-
这对于像 Chalkduster 这样的字体来说非常慢
标签: ios objective-c iphone cocoa-touch core-graphics