Center text if line height isn't 0
Summary: changelog: Fix vertical text alignment in new architecture when line height is not 0. Reviewed By: javache Differential Revision: D40346225 fbshipit-source-id: f6282cb8df80e9a543e5602fddcca1a1a82377e3
This commit is contained in:
Родитель
712fcdec9d
Коммит
70cc27c901
|
@ -305,6 +305,50 @@ NSDictionary<NSAttributedStringKey, id> *RCTNSTextAttributesFromTextAttributes(T
|
|||
return [attributes copy];
|
||||
}
|
||||
|
||||
static void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText)
|
||||
{
|
||||
__block CGFloat maximumLineHeight = 0;
|
||||
|
||||
[attributedText enumerateAttribute:NSParagraphStyleAttributeName
|
||||
inRange:NSMakeRange(0, attributedText.length)
|
||||
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
|
||||
usingBlock:^(NSParagraphStyle *paragraphStyle, __unused NSRange range, __unused BOOL *stop) {
|
||||
if (!paragraphStyle) {
|
||||
return;
|
||||
}
|
||||
|
||||
maximumLineHeight = MAX(paragraphStyle.maximumLineHeight, maximumLineHeight);
|
||||
}];
|
||||
|
||||
if (maximumLineHeight == 0) {
|
||||
// `lineHeight` was not specified, nothing to do.
|
||||
return;
|
||||
}
|
||||
|
||||
__block CGFloat maximumFontLineHeight = 0;
|
||||
|
||||
[attributedText enumerateAttribute:NSFontAttributeName
|
||||
inRange:NSMakeRange(0, attributedText.length)
|
||||
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
|
||||
usingBlock:^(UIFont *font, NSRange range, __unused BOOL *stop) {
|
||||
if (!font) {
|
||||
return;
|
||||
}
|
||||
|
||||
maximumFontLineHeight = MAX(font.lineHeight, maximumFontLineHeight);
|
||||
}];
|
||||
|
||||
if (maximumLineHeight < maximumFontLineHeight) {
|
||||
return;
|
||||
}
|
||||
|
||||
CGFloat baseLineOffset = (maximumLineHeight - maximumFontLineHeight) / 2.0;
|
||||
|
||||
[attributedText addAttribute:NSBaselineOffsetAttributeName
|
||||
value:@(baseLineOffset)
|
||||
range:NSMakeRange(0, attributedText.length)];
|
||||
}
|
||||
|
||||
NSAttributedString *RCTNSAttributedStringFromAttributedString(const AttributedString &attributedString)
|
||||
{
|
||||
static UIImage *placeholderImage;
|
||||
|
@ -357,7 +401,7 @@ NSAttributedString *RCTNSAttributedStringFromAttributedString(const AttributedSt
|
|||
|
||||
[nsAttributedString appendAttributedString:nsAttributedStringFragment];
|
||||
}
|
||||
|
||||
RCTApplyBaselineOffset(nsAttributedString);
|
||||
[nsAttributedString endEditing];
|
||||
|
||||
return nsAttributedString;
|
||||
|
|
Загрузка…
Ссылка в новой задаче