We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
文本的Paragraph Style设置居中,在计算布局的时候,左侧缩进后右侧并没有缩进。这将导致计算的结果实际上是居右的。
修改后的代码
新增了一个在有居中的情况下的补偿宽度。另外 _trailingWhitespaceWidth这个东西干啥用的,有点烟雾弹的效果- -。感觉像是你写漏了。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
文本的Paragraph Style设置居中,在计算布局的时候,左侧缩进后右侧并没有缩进。这将导致计算的结果实际上是居右的。
嘛 算了我描述好像不太清楚。总之就是设置居中看起来像是居右。
原始代码:
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];
}
}
修改后的代码
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这个东西干啥用的,有点烟雾弹的效果- -。感觉像是你写漏了。
The text was updated successfully, but these errors were encountered: