如何处理 iPhone 代码中的屏幕位置常量?

发布于 2024-09-18 03:53:38 字数 1299 浏览 3 评论 0原文

我需要用不同长度的环绕文本填充单元格。我当前的例程处理得令人满意,但是,我担心正在使用的几个常量的使用。

下面的值 10 和 260 代表预期的边距和单元格宽度,但它们仅适用于纵向的标准清晰度分辨率。

是否有一些屏幕/表格对象为我提供这些值作为我可以使用而不是常量的指标?

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell;
    UILabel *lbl;
    CGRect frame;

    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
                                   reuseIdentifier:AnswerIdentifier] autorelease];

    frame.origin.x = 10;
    frame.origin.y = 10;
    frame.size.height = [self textHeight:indexPath];
    frame.size.width = 260;

    lbl = [[UILabel alloc] initWithFrame:frame];
    lbl.lineBreakMode = UILineBreakModeWordWrap;
    lbl.numberOfLines = 0;         
    [cell.contentView addSubview:lbl];
    [lbl release];

    return cell;

}

- (CGFloat)textHeight:(NSIndexPath *)indexPath {

    CGSize textSize;
    CGSize constraintSize;
    CGFloat height;

    NSString *theText = [self cellText:indexPath];

    constraintSize = CGSizeMake(260.0f, MAXFLOAT);

    textSize = [theText sizeWithFont:kCELL_FONT 
                   constrainedToSize:constraintSize 
                       lineBreakMode:UILineBreakModeWordWrap];
    height = textSize.height;

    return height;

}

I have a requirement to fill cells with varying length wraparound text. My current routine is handling this satisfactorily, however, I am concerned about the use of a couple of constants being used.

The values 10 and 260 below represent the margin and cell width expected, but they are only accurate for standard definition resolution in portrait orientation.

Is there some screen/table object that provides me these values as metrics I could use instead of the constants?

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell;
    UILabel *lbl;
    CGRect frame;

    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
                                   reuseIdentifier:AnswerIdentifier] autorelease];

    frame.origin.x = 10;
    frame.origin.y = 10;
    frame.size.height = [self textHeight:indexPath];
    frame.size.width = 260;

    lbl = [[UILabel alloc] initWithFrame:frame];
    lbl.lineBreakMode = UILineBreakModeWordWrap;
    lbl.numberOfLines = 0;         
    [cell.contentView addSubview:lbl];
    [lbl release];

    return cell;

}

- (CGFloat)textHeight:(NSIndexPath *)indexPath {

    CGSize textSize;
    CGSize constraintSize;
    CGFloat height;

    NSString *theText = [self cellText:indexPath];

    constraintSize = CGSizeMake(260.0f, MAXFLOAT);

    textSize = [theText sizeWithFont:kCELL_FONT 
                   constrainedToSize:constraintSize 
                       lineBreakMode:UILineBreakModeWordWrap];
    height = textSize.height;

    return height;

}

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

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

发布评论

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

评论(2

几度春秋 2024-09-25 03:53:38

该表显然需要适合包含视图的框架(可能是顶级视图或窗口),因此我可能会使用该矩形的百分比或像素偏移量。

The table obviously needs to fit in the frame of the containing view (which might be the top level view or the window), so I might use percentage or pixel offsets from that rect.

我家小可爱 2024-09-25 03:53:38

尝试使用

[[UIScreen mainScreen] applicationBounds]; 

这将返回当前窗口大小的 CGRect 。然后,对于您的代码,您可以使用:

CGRect screenRect = [[UIScreen mainScreen] applicationBounds]; 
frame.origin.x = 10;
frame.origin.y = 10;
frame.size.height = [self textHeight:indexPath];
frame.size.width = screenRect.size.width - 60.0;

假设 x 中的像素偏移是固定的,并且标签宽度灵活。

Try using

[[UIScreen mainScreen] applicationBounds]; 

This will return a CGRect for the current window size. Then, for your code, you might use:

CGRect screenRect = [[UIScreen mainScreen] applicationBounds]; 
frame.origin.x = 10;
frame.origin.y = 10;
frame.size.height = [self textHeight:indexPath];
frame.size.width = screenRect.size.width - 60.0;

Assuming that the pixel offsets in x are fixed, with flexible label width.

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