X和Y值未应用于标签

时间:2017-05-12 09:31:00

标签: ios uilabel drawrect

我正在尝试在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值不适用于标签:

X and Y values are not applied

它应该用红色矩形指定

3 个答案:

答案 0 :(得分:1)

我认为您必须再次布局视图,因为您正在更新标签框架。 试试这个

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

更新帧后添加这些行。 我认为这应该有用。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

https://stackoverflow.com/a/2697483/614065

要点是:

  

如果在视图中执行自定义绘图,则不得绘制UILabel   或另一个UI元素,而是自己绘制文本。

这是我的完整代码:

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