Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YYTextLayout中计算_lineWidth的代码直接使用了CTLineGetTypographicBounds,这将在文本设置居中的时候出现一些布局的错误 #983

Open
CodeSlaveZhang opened this issue May 24, 2023 · 0 comments

Comments

@CodeSlaveZhang
Copy link

CodeSlaveZhang commented May 24, 2023

文本的Paragraph Style设置居中,在计算布局的时候,左侧缩进后右侧并没有缩进。这将导致计算的结果实际上是居右的。

嘛 算了我描述好像不太清楚。总之就是设置居中看起来像是居右。
原始代码:

  • (void)setCTLine:(_Nonnull CTLineRef)CTLine {
    if (_CTLine != CTLine) {
    if (CTLine) CFRetain(CTLine);
    if (_CTLine) CFRelease(_CTLine);
    _CTLine = CTLine;
    if (_CTLine) {
    _lineWidth = CTLineGetTypographicBounds(_CTLine, &_ascent, &_descent, &_leading);
    CFRange range = CTLineGetStringRange(_CTLine);
    _range = NSMakeRange(range.location, range.length);
    if (CTLineGetGlyphCount(_CTLine) > 0) {
    CFArrayRef runs = CTLineGetGlyphRuns(_CTLine);
    CTRunRef run = CFArrayGetValueAtIndex(runs, 0);
    CGPoint pos;
    CTRunGetPositions(run, CFRangeMake(0, 1), &pos);
    _firstGlyphPos = pos.x;
    } else {
    _firstGlyphPos = 0;
    }
    _trailingWhitespaceWidth = CTLineGetTrailingWhitespaceWidth(_CTLine);
    } else {
    _lineWidth = _ascent = _descent = _leading = _firstGlyphPos = _trailingWhitespaceWidth = 0;
    _range = NSMakeRange(0, 0);
    }
    [self reloadBounds];
    }
    }

修改后的代码

  • (void)setCTLine:(_Nonnull CTLineRef)CTLine {
    if (_CTLine != CTLine) {
    if (CTLine) CFRetain(CTLine);
    if (_CTLine) CFRelease(_CTLine);
    _CTLine = CTLine;
    if (_CTLine) {
    _lineWidth = CTLineGetTypographicBounds(_CTLine, &_ascent, &_descent, &_leading);
    CFRange range = CTLineGetStringRange(_CTLine);
    _range = NSMakeRange(range.location, range.length);
    if (CTLineGetGlyphCount(_CTLine) > 0) {
    CFArrayRef runs = CTLineGetGlyphRuns(_CTLine);
    CTRunRef run = CFArrayGetValueAtIndex(runs, 0);
    CGPoint pos;
    CTRunGetPositions(run, CFRangeMake(0, 1), &pos);
    _firstGlyphPos = pos.x;
    **///查看是否有文字居中
    NSDictionary attrs = (id)CTRunGetAttributes(run);
    NSParagraphStyle paraStyle = attrs[NSParagraphStyleAttributeName];
    if (paraStyle.alignment == NSTextAlignmentCenter) {
    //居中的时候line的右边空白要保持和左边一致
    _lineWidth += _position.x;
    }

    } else {
    _firstGlyphPos = 0;
    }
    _trailingWhitespaceWidth = CTLineGetTrailingWhitespaceWidth(_CTLine);
    } else {
    _lineWidth = _ascent = _descent = _leading = _firstGlyphPos = _trailingWhitespaceWidth = 0;
    _range = NSMakeRange(0, 0);
    }
    [self reloadBounds];
    }
    }

新增了一个在有居中的情况下的补偿宽度。另外 _trailingWhitespaceWidth这个东西干啥用的,有点烟雾弹的效果- -。感觉像是你写漏了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant