CRToast icon indicating copy to clipboard operation
CRToast copied to clipboard

Center text properly when an image is being displayed

Open cruffenach opened this issue 12 years ago • 1 comments

Currently it is centered in its available bounds but not centered in the notification

cruffenach avatar Feb 13 '14 07:02 cruffenach

Not sure of the elegance of the solution I thought of two solutions that ideally would work without having to subclass UILabel

  1. inset both sides of the label by image width
  2. ignore image width if text is centered

Not sure how viable they are or if they're even really worth doing, however i threw option 2 in the test project and it seems to work. I just modified CRToast -layoutSubview specifically lines 1053-1088 hold all the changes.

    CGFloat titleLabelX = (imageSize.width == 0 || self.toast.textAlignment == NSTextAlignmentCenter) ? kCRStatusBarViewNoImageLeftContentInset : CGRectGetMaxX(_imageView.frame);
    CGFloat subTitleLabelX = (imageSize.width == 0 || self.toast.subtitleTextAlignment == NSTextAlignmentCenter) ? kCRStatusBarViewNoImageLeftContentInset : CGRectGetMaxX(_imageView.frame);

    CGFloat x = imageSize.width == 0 ? kCRStatusBarViewNoImageLeftContentInset : CGRectGetMaxX(_imageView.frame);
    CGFloat width = CGRectGetWidth(contentFrame)-x-kCRStatusBarViewNoImageRightContentInset;

    if (self.toast.subtitleText == nil) {
        self.label.frame = CGRectMake(titleLabelX,
                                      statusBarYOffset,
                                      width,
                                      CGRectGetHeight(contentFrame));
    } else {
        CGFloat height = MIN([self.toast.text boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                                           options:NSStringDrawingUsesLineFragmentOrigin
                                                        attributes:@{NSFontAttributeName : self.toast.font}
                                                           context:nil].size.height,
                             CGRectGetHeight(contentFrame));
        CGFloat subtitleHeight = [self.toast.subtitleText boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                                                       options:NSStringDrawingUsesLineFragmentOrigin
                                                                    attributes:@{NSFontAttributeName : self.toast.subtitleFont }
                                                                       context:nil].size.height;
        if ((CGRectGetHeight(contentFrame) - (height + subtitleHeight)) < 5) {
            subtitleHeight = (CGRectGetHeight(contentFrame) - (height))-10;
        }
        CGFloat offset = (CGRectGetHeight(contentFrame) - (height + subtitleHeight))/2;

        self.label.frame = CGRectMake(titleLabelX,
                                      offset+statusBarYOffset,
                                      CGRectGetWidth(contentFrame)-titleLabelX-kCRStatusBarViewNoImageRightContentInset,
                                      height);


        self.subtitleLabel.frame = CGRectMake(subTitleLabelX,
                                              height+offset+statusBarYOffset,
                                              CGRectGetWidth(contentFrame)-subTitleLabelX-kCRStatusBarViewNoImageRightContentInset,
                                              subtitleHeight);
    }

There's probably a better way but ¯\_(ツ)_/¯

dmiedema avatar Dec 01 '14 05:12 dmiedema