X 和 Y 值未应用于标签

X and Y values not applied to label

我正在尝试在 drawRect 中绘制标签。当数据集时,我使用 sizeToFit 函数计算标签高度,然后使用所需的 X 和 Y 值重新创建标签的框架。

在数据集上:

- (void)setNewsDetailDto:(NewsDetailDto *)newsDetailDto {
    _newsDetailDto = newsDetailDto;
    MediaDto *mediaDto = newsDetailDto.media[0];
    NSLog(@"UIBPVCell - setNewsDetailDto - URL : %@", mediaDto.media);
    NSURL *url = [NSURL URLWithString:mediaDto.media];
    [[SDWebImageDownloader sharedDownloader]
            downloadImageWithURL:url
                         options:SDWebImageDownloaderContinueInBackground | SDWebImageDownloaderHighPriority
                        progress:nil
                       completed:^(UIImage *_Nullable image, NSData *_Nullable data, NSError *_Nullable error, BOOL finished) {
                           CGSize scaleSize = CGSizeMake(image.size.width * screenWidth / image.size.height, screenWidth);
                           _newsImage = [self imageWithImage:image scaledToSize:scaleSize];
                           CGFloat x = (CGFloat) fabs(image.size.width - newsImageRect.size.width) / 2;
                           CGRect frame = CGRectMake(x, 0, screenWidth, screenWidth);
                           _newsImage = [self croppedImage:_newsImage inRect:frame];

                           labelTitle = [[UILabel alloc] init];
                           labelTitle.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:27.0f];
                           labelTitle.text = newsDetailDto.title;
                           labelTitle.textColor = [UIColor whiteColor];
                           labelTitle.numberOfLines = 0;
                           labelTitle.textAlignment = NSTextAlignmentLeft;
                           labelTitle.preferredMaxLayoutWidth = screenWidth - 36.0f;

                           [self setNeedsLayout];
                           [self setNeedsDisplay];
                       }];
}

- (void)layoutSubviews {
    NSLog(@"UIBPVCell : layoutSubviews");
    [super layoutSubviews];
    [self calculateRects];
}

- (void)calculateRects {
    screenWidth = [UIScreen mainScreen].bounds.size.width;
    newsImageRect = CGRectMake(0, 0, screenWidth, screenWidth);

    if (labelTitle) {
        [labelTitle sizeToFit];
        CGRect frame =
                CGRectMake(
                        13,
                        floor(screenWidth - labelTitle.frame.size.height - 10),
                        screenWidth - 36,
                        ceil(labelTitle.frame.size.height));
        labelTitle.frame = frame;
    }
}

在 drawRect 中:

- (void)drawRect:(CGRect)rect {
    NSLog(@"UIBPVCell : drawRect");
    [super drawRect:rect];
    [labelTitle drawTextInRect:rect];
}

但 X 和 Y 值未应用于标签:

它应该在用红色矩形指定的地方

我想你必须重新布局视图,因为你正在更新标签的框架。 试试这个

[[self view] setNeedsLayout];
[[self view] layoutIfNeeded];

更新框架后添加这些行。 我认为这应该有效。

使用label.layer.clipToBounds=YES。然后给你的标签一些背景颜色并相应地调整你的标签。 希望这有帮助!!

我从这个问题中找到了答案:

要点是:

If you perform custom drawing in your view you must not draw UILabel or another UI element but rather draw text itself.

这是我的完整代码:

NSMutableParagraphStyle *paragraphStyle = 
            [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;

    UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:27.0f];

    attributes = @{
            NSFontAttributeName: font,
            NSForegroundColorAttributeName: [UIColor whiteColor],
            NSParagraphStyleAttributeName: paragraphStyle };

    titleSize = [_newsDetailDto.title
            boundingRectWithSize:CGSizeMake(screenWidth - 36, 0)
                         options:NSStringDrawingUsesLineFragmentOrigin
                      attributes:attributes
                         context:nil].size;

    titleSize = CGSizeMake(ceilf(titleSize.width), ceilf(titleSize.height));
    [self setNeedsLayout];
    [self layoutIfNeeded];

在 drawRect 中:

- (void)drawRect:(CGRect)rect {
    NSLog(@"UIBPVCell : drawRect");
    [super drawRect:rect];

    ...

    if (_newsDetailDto) {
        [_newsDetailDto.title
                drawInRect:titleRect
            withAttributes:attributes];
    }
}