Olcay Ertaş
Olcay Ertaş

Reputation: 6228

X and Y values not applied to label

I am trying to draw a label in drawRect. When data set I am calculating label height by using sizeToFit function then recreating label's frame with required X and Y values.

On data set:

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

In drawRect:

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

But X and Y values are not applied to label:

X and Y values are not applied

It should be in place specified with red rectangle

Upvotes: 0

Views: 49

Answers (3)

Olcay Ertaş
Olcay Ertaş

Reputation: 6228

I have found my answer from this question:

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

Main point is:

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

And here is my complete code:

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

In drawRect:

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

    ...

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

Upvotes: 0

Darshan Karekar
Darshan Karekar

Reputation: 681

Use label.layer.clipToBounds=YES. Then give your label some background color and adjust your label accordingly. Hope this helps !!

Upvotes: 0

Khushal Dugar
Khushal Dugar

Reputation: 231

I think you have to layout the view again since you are updating the frame of label. Try this

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

add these lines after updating frame. I think this should work.

Upvotes: 1

Related Questions