Skip to content

Commit 90b116a

Browse files
committed
Adjust line height calculation
1 parent 840c31c commit 90b116a

File tree

2 files changed

+68
-84
lines changed

2 files changed

+68
-84
lines changed

packages/react-native/Libraries/Text/Text/RCTTextShadowView.mm

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -123,48 +123,40 @@ - (void)uiManagerWillPerformMounting
123123

124124
- (void)postprocessAttributedText:(NSMutableAttributedString *)attributedText
125125
{
126-
__block CGFloat maximumLineHeight = 0;
127-
128-
[attributedText enumerateAttribute:NSParagraphStyleAttributeName
129-
inRange:NSMakeRange(0, attributedText.length)
130-
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
131-
usingBlock:^(NSParagraphStyle *paragraphStyle, __unused NSRange range, __unused BOOL *stop) {
132-
if (!paragraphStyle) {
133-
return;
134-
}
135-
136-
maximumLineHeight = MAX(paragraphStyle.maximumLineHeight, maximumLineHeight);
137-
}];
138-
139-
if (maximumLineHeight == 0) {
140-
// `lineHeight` was not specified, nothing to do.
141-
return;
142-
}
143-
144-
__block CGFloat maximumFontLineHeight = 0;
145-
146-
[attributedText enumerateAttribute:NSFontAttributeName
147-
inRange:NSMakeRange(0, attributedText.length)
148-
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
149-
usingBlock:^(UIFont *font, NSRange range, __unused BOOL *stop) {
150-
if (!font) {
151-
return;
152-
}
153-
154-
if (maximumFontLineHeight <= font.lineHeight) {
155-
maximumFontLineHeight = font.lineHeight;
156-
}
157-
}];
158-
159-
if (maximumLineHeight < maximumFontLineHeight) {
160-
return;
161-
}
162-
163-
CGFloat baseLineOffset = maximumLineHeight / 2.0 - maximumFontLineHeight / 2.0;
164-
165-
[attributedText addAttribute:NSBaselineOffsetAttributeName
166-
value:@(baseLineOffset)
167-
range:NSMakeRange(0, attributedText.length)];
126+
// Retrieve paragraph line height value
127+
__block CGFloat paragraphLineHeight = 0;
128+
[attributedText enumerateAttribute:NSParagraphStyleAttributeName
129+
inRange:NSMakeRange(0, attributedText.length)
130+
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
131+
usingBlock:^(NSParagraphStyle *paragraphStyle, __unused NSRange range, __unused BOOL *stop) {
132+
if (!paragraphStyle) {
133+
return;
134+
}
135+
136+
paragraphLineHeight = paragraphStyle.maximumLineHeight;
137+
}];
138+
139+
// Retrieve font size and font line height values
140+
__block CGFloat fontSize = 0;
141+
__block CGFloat fontLineHeight = 0;
142+
[attributedText enumerateAttribute:NSFontAttributeName
143+
inRange:NSMakeRange(0, attributedText.length)
144+
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
145+
usingBlock:^(UIFont *font, NSRange range, __unused BOOL *stop) {
146+
if (!font) {
147+
return;
148+
}
149+
150+
fontSize = font.pointSize;
151+
fontLineHeight = font.lineHeight;
152+
}];
153+
154+
// Calculate baseline offset based on font size and maximum available line height value
155+
CGFloat maximumLineHeight = MAX(paragraphLineHeight, fontLineHeight);
156+
CGFloat baselineOffset = (fontSize - maximumLineHeight) / 1.5;
157+
[attributedText addAttribute:NSBaselineOffsetAttributeName
158+
value:@(baselineOffset)
159+
range:NSMakeRange(0, attributedText.length)];
168160
}
169161

170162
- (NSAttributedString *)attributedTextWithMeasuredAttachmentsThatFitSize:(CGSize)size

packages/react-native/Libraries/Text/TextInput/RCTBaseTextInputShadowView.mm

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -195,48 +195,40 @@ - (void)uiManagerWillPerformMounting
195195

196196
- (void)postprocessAttributedText:(NSMutableAttributedString *)attributedText
197197
{
198-
__block CGFloat maximumLineHeight = 0;
199-
200-
[attributedText enumerateAttribute:NSParagraphStyleAttributeName
201-
inRange:NSMakeRange(0, attributedText.length)
202-
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
203-
usingBlock:^(NSParagraphStyle *paragraphStyle, __unused NSRange range, __unused BOOL *stop) {
204-
if (!paragraphStyle) {
205-
return;
206-
}
207-
208-
maximumLineHeight = MAX(paragraphStyle.maximumLineHeight, maximumLineHeight);
209-
}];
210-
211-
if (maximumLineHeight == 0) {
212-
// `lineHeight` was not specified, nothing to do.
213-
return;
214-
}
215-
216-
__block CGFloat maximumFontLineHeight = 0;
217-
218-
[attributedText enumerateAttribute:NSFontAttributeName
219-
inRange:NSMakeRange(0, attributedText.length)
220-
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
221-
usingBlock:^(UIFont *font, NSRange range, __unused BOOL *stop) {
222-
if (!font) {
223-
return;
224-
}
225-
226-
if (maximumFontLineHeight <= font.lineHeight) {
227-
maximumFontLineHeight = font.lineHeight;
228-
}
229-
}];
230-
231-
if (maximumLineHeight < maximumFontLineHeight) {
232-
return;
233-
}
234-
235-
CGFloat baseLineOffset = maximumLineHeight / 2.0 - maximumFontLineHeight / 2.0;
236-
237-
[attributedText addAttribute:NSBaselineOffsetAttributeName
238-
value:@(baseLineOffset)
239-
range:NSMakeRange(0, attributedText.length)];
198+
// Retrieve paragraph line height value
199+
__block CGFloat paragraphLineHeight = 0;
200+
[attributedText enumerateAttribute:NSParagraphStyleAttributeName
201+
inRange:NSMakeRange(0, attributedText.length)
202+
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
203+
usingBlock:^(NSParagraphStyle *paragraphStyle, __unused NSRange range, __unused BOOL *stop) {
204+
if (!paragraphStyle) {
205+
return;
206+
}
207+
208+
paragraphLineHeight = paragraphStyle.maximumLineHeight;
209+
}];
210+
211+
// Retrieve font size and font line height values
212+
__block CGFloat fontSize = 0;
213+
__block CGFloat fontLineHeight = 0;
214+
[attributedText enumerateAttribute:NSFontAttributeName
215+
inRange:NSMakeRange(0, attributedText.length)
216+
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
217+
usingBlock:^(UIFont *font, NSRange range, __unused BOOL *stop) {
218+
if (!font) {
219+
return;
220+
}
221+
222+
fontSize = font.pointSize;
223+
fontLineHeight = font.lineHeight;
224+
}];
225+
226+
// Calculate baseline offset based on font size and maximum available line height value
227+
CGFloat maximumLineHeight = MAX(paragraphLineHeight, fontLineHeight);
228+
CGFloat baselineOffset = (fontSize - maximumLineHeight) / 1.5;
229+
[attributedText addAttribute:NSBaselineOffsetAttributeName
230+
value:@(baselineOffset)
231+
range:NSMakeRange(0, attributedText.length)];
240232
}
241233

242234
#pragma mark -

0 commit comments

Comments
 (0)