开发者

Unable to drawTextInRect with UILabel

I want to draw a UILabel onto my cell. I don't want to add it as a subview for performance reasons. I am doing the following:

-(void)drawContent
{
    usernameLabel.text = [message valueForKey:@"user_login"];
    usernameLabel.font = [UIFont fontW开发者_JAVA技巧ithName:@"HelveticaNeue-Bold" size:12];
    usernameLabel.textColor = [UIColor blackColor];
    [usernameLabel drawTextInRect:CGRectMake(77, 5, 200, 20)];
}

but I get a ton of errors:

<Error>: CGContextSetFillColorWithColor: invalid context 0x0
<Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
<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


You should really call any drawing methods in a UIView's drawRect: implementation. If I remember correctly, UITableViewCells used to support drawing directly where you could do the following in your UITableViewCell subclass:

- (void) drawRect: (CGRect) rect
{
    [self drawContent];
}

This is now a discouraged practice for a variety of reasons. However, you can still create a custom UIView subclass which draws the actual cell content, add it to the cell's contentView and have it implement drawRect: inside which you send drawTextInRect:. That will work as expected.


To draw an UILabel, just create and add the UILabel to the cells contentView within the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method.

There is no need to call drawTextInRect: directly. This only should be used on subclassing UILabel cell and overwriting that method with an alternative implementation. Calling the method directly as in your code causes these errors, because the system has not setup the graphics context to draw to.

EDIT Update with sample reference:

If you want to draw a table's cell on your own, check the TableViewSuite provided by Apple. In there you will finde the 5_CustomTableViewCell/Classes/TimeZoneView which is used as a content subview for the 5_CustomTableViewCell/Classes/TimeZoneCell. The TimeZoneView shows how to draw your custom text directly to the view without using an UILabel.


I never tried using drawTextInect, but you can try this way:

 cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, cell.frame.size.width,   cell.frame.size.height)];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
cellLabel.backgroundColor = [UIColor clearColor];
cellLabel.font = [UIFont boldSystemFontOfSize:14];
cellLabel.lineBreakMode = UILineBreakModeWordWrap;
[cell.contentView addSubview:cellLabel];

Hope this help!!


Have you read the method's documentation?

This method is only for subclassing and you are getting the errors because there is no drawing context defined and this pertain's to the UILabel instance and not where drawContent is being called. If you wish to draw a text in a different view then use an NSString instance. It provides drawing extensions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜