无法使用 UILabel 绘制TextInRect

发布于 2024-11-17 08:18:03 字数 839 浏览 6 评论 0原文

我想在我的单元格上绘制一个 UILabel。出于性能原因,我不想将其添加为子视图。我正在执行以下操作:

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

但出现大量错误:

<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

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 fontWithName:@"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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只是我以为 2024-11-24 08:18:03

您确实应该在 UIView 的 drawRect: 实现中调用任何绘图方法。如果我没记错的话,UITableViewCells 曾经支持直接绘图,您可以在 UITableViewCell 子类中执行以下操作:

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

由于多种原因,现在不鼓励这样做。但是,您仍然可以创建一个自定义 UIView 子类来绘制实际的单元格内容,将其添加到单元格的 contentView 中,并让它实现您在其中发送 drawRect:绘制文本矩形:。这将按预期工作。

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.

淡淡的优雅 2024-11-24 08:18:03

要绘制 UILabel,只需在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中创建 UILabel 并将其添加到单元格 contentView 中。

无需直接调用drawTextInRect:。这只应用于子类化 UILabel 单元并使用替代实现覆盖该方法。在代码中直接调用该方法会导致这些错误,因为系统尚未设置要绘制的图形上下文。

编辑使用示例参考进行更新:

如果您想自己绘制表格的单元格,请检查TableViewSuite 由提供 苹果。在那里您将找到 5_CustomTableViewCell/Classes/TimeZoneView,它用作 5_CustomTableViewCell/Classes/TimeZoneCell 的内容子视图。 TimeZoneView 展示了如何在不使用 UILabel 的情况下将自定义文本直接绘制到视图中。

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.

雅心素梦 2024-11-24 08:18:03

我从未尝试过使用drawTextInect,但你可以尝试这种方式:

 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];

希望这有帮助!

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!!

影子是时光的心 2024-11-24 08:18:03

您是否阅读过该方法的 文档

此方法仅用于子类化,并且您会收到错误,因为没有定义绘图上下文,并且这属于 UILabel 实例,而不是调用 drawContent 的位置。如果您希望在不同的视图中绘制文本,请使用 NSString 实例。它提供绘图扩展

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文