ibireme / YYText

Powerful text framework for iOS to display and edit rich text.
MIT License
8.84k stars 1.69k forks source link

Position of Placeholder didn't align to the cursor #865

Open starFelix opened 5 years ago

starFelix commented 5 years ago

if I set the textVerticalAlignment to center, the position of placeholder would not change.

image

I thought the problem is as below:

- (void)_updatePlaceholder {
    CGRect frame = CGRectZero;
    _placeHolderView.image = nil;
    _placeHolderView.frame = frame;
    if (_placeholderAttributedText.length > 0) {
        YYTextContainer *container = _innerContainer.copy;
        container.size = self.bounds.size;
        container.truncationType = YYTextTruncationTypeEnd;
        container.truncationToken = nil;
        YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:_placeholderAttributedText];
        CGSize size = [layout textBoundingSize];
        BOOL needDraw = size.width > 1 && size.height > 1;
        if (needDraw) {
            UIGraphicsBeginImageContextWithOptions(size, NO, 0);
            CGContextRef context = UIGraphicsGetCurrentContext();
            [layout drawInContext:context size:size debug:self.debugOption];
            UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            _placeHolderView.image = image;
            frame.size = image.size;
            if (container.isVerticalForm) {
                frame.origin.x = self.bounds.size.width - image.size.width;
            } else {
                frame.origin = CGPointZero;
            }
            _placeHolderView.frame = frame;
        }
    }
}
shenguanjiejie commented 5 years ago

Set placeholderFont to solve~

starFelix commented 5 years ago

Set placeholderFont to solve~

I don't think it can solve this problem. The size of placeholderFont should be the same as the size of textview.font.