我在drawRect:
中給UIVIew畫了一些文字。首先我計算文本高度,然後drawInRect:
。代碼波紋管工作:drawInRect error:CGContextSetFont:invalid context 0x0
- (void)drawRect:(CGRect)rect
{
CGFloat titleHeight = [self heightForText:_entry.title
withFont:[UIFont systemFontOfSize:12.0f]];
CGRect r = CGRectMake(54, 6, kCellTextWidth, titleHeight);
[_entry.title drawInRect:r withFont:[UIFont titleFont]];
}
然後我計算文本高度與main_queue dispatch_async
和drawInRect,它失敗:
- (void)drawRect:(CGRect)rect
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
CGFloat titleHeight = [self heightForText:_entry.title
withFont:[UIFont systemFontOfSize:12.0f]];
dispatch_async(dispatch_get_main_queue(), ^{
CGRect r = CGRectMake(54, 6, kCellTextWidth, titleHeight);
[_entry.title drawInRect:r withFont:[UIFont titleFont]];
});
});
}
錯誤:
<Error>: CGContextSetFont: invalid context 0x0
<Error>: CGContextSetTextMatrix: invalid context 0x0
<Error>: CGContextSetFontSize: invalid context 0x0
<Error>: CGContextSetTextPosition: invalid context 0x0
<Error>: CGContextShowGlyphsWithAdvances: invalid context 0x0
什麼錯誤呢? 如何在另一個線程中計算文本高度以提高速度?
謝謝。
由於dispatch_async塊**不允許對UI進行更改** –