Skip to content

Commit ada9654

Browse files
authored
fix(iOS): nil NSParagraphStyle crashes (#450)
1 parent 4475066 commit ada9654

File tree

3 files changed

+31
-45
lines changed

3 files changed

+31
-45
lines changed

ios/EnrichedTextInputView.mm

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -291,50 +291,6 @@ - (void)setupPlaceholderLabel {
291291
_placeholderLabel.adjustsFontForContentSizeCategory = YES;
292292
}
293293

294-
// MARK: - Paragraph style helpers
295-
296-
- (void)applyLineHeight:(NSMutableDictionary *)typingAttributes {
297-
CGFloat rawLineHeight = [config primaryLineHeight];
298-
NSUInteger length = textView.textStorage.string.length;
299-
if (length == 0) {
300-
return;
301-
}
302-
303-
CGFloat scaledLineHeight = 0;
304-
if (rawLineHeight > 0) {
305-
// Scale lineHeight with the same Dynamic Type factor used for font sizes.
306-
scaledLineHeight =
307-
[[UIFontMetrics defaultMetrics] scaledValueForValue:rawLineHeight];
308-
}
309-
310-
NSRange fullRange = NSMakeRange(0, length);
311-
312-
// apply lineHeight over the entire text storage content
313-
[textView.textStorage
314-
enumerateAttribute:NSParagraphStyleAttributeName
315-
inRange:fullRange
316-
options:0
317-
usingBlock:^(id _Nullable value, NSRange range,
318-
BOOL *_Nonnull stop) {
319-
NSMutableParagraphStyle *pStyle;
320-
if (value != nil) {
321-
pStyle = [(NSParagraphStyle *)value mutableCopy];
322-
} else {
323-
pStyle = [[NSMutableParagraphStyle alloc] init];
324-
}
325-
pStyle.minimumLineHeight = scaledLineHeight;
326-
[textView.textStorage addAttribute:NSParagraphStyleAttributeName
327-
value:pStyle
328-
range:range];
329-
}];
330-
331-
// apply lineHeight to typing attributes
332-
NSMutableParagraphStyle *paragraphStyle =
333-
[[NSMutableParagraphStyle alloc] init];
334-
paragraphStyle.minimumLineHeight = scaledLineHeight;
335-
typingAttributes[NSParagraphStyleAttributeName] = paragraphStyle;
336-
}
337-
338294
// MARK: - Props
339295

340296
- (void)updateProps:(Props::Shared const &)props
@@ -781,10 +737,16 @@ - (void)updateProps:(Props::Shared const &)props
781737
[config primaryColor];
782738
defaultTypingAttributes[NSStrikethroughColorAttributeName] =
783739
[config primaryColor];
784-
[self applyLineHeight:defaultTypingAttributes];
740+
NSMutableParagraphStyle *defaultPStyle =
741+
[[NSMutableParagraphStyle alloc] init];
742+
defaultPStyle.minimumLineHeight = [config scaledPrimaryLineHeight];
743+
defaultTypingAttributes[NSParagraphStyleAttributeName] = defaultPStyle;
744+
785745
textView.typingAttributes = defaultTypingAttributes;
786746
textView.selectedRange = prevSelectedRange;
787747

748+
// make sure the newest lineHeight is applied
749+
[self refreshLineHeight];
788750
// update the placeholder as well
789751
[self refreshPlaceholderLabelStyles];
790752
}
@@ -954,6 +916,24 @@ - (void)refreshPlaceholderLabelStyles {
954916
_placeholderLabel.attributedText = newAttrStr;
955917
}
956918

919+
- (void)refreshLineHeight {
920+
[textView.textStorage
921+
enumerateAttribute:NSParagraphStyleAttributeName
922+
inRange:NSMakeRange(0, textView.textStorage.string.length)
923+
options:0
924+
usingBlock:^(id _Nullable value, NSRange range,
925+
BOOL *_Nonnull stop) {
926+
NSMutableParagraphStyle *pStyle =
927+
[(NSParagraphStyle *)value mutableCopy];
928+
if (pStyle == nil)
929+
return;
930+
pStyle.minimumLineHeight = [config scaledPrimaryLineHeight];
931+
[textView.textStorage addAttribute:NSParagraphStyleAttributeName
932+
value:pStyle
933+
range:range];
934+
}];
935+
}
936+
957937
// MARK: - Measuring and states
958938

959939
- (CGSize)measureSize:(CGFloat)maxWidth {

ios/config/InputConfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- (void)setPrimaryFontSize:(NSNumber *)newValue;
1313
- (CGFloat)primaryLineHeight;
1414
- (void)setPrimaryLineHeight:(CGFloat)newValue;
15+
- (CGFloat)scaledPrimaryLineHeight;
1516
- (NSString *)primaryFontWeight;
1617
- (void)setPrimaryFontWeight:(NSString *)newValue;
1718
- (NSString *)primaryFontFamily;

ios/config/InputConfig.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ - (void)setPrimaryLineHeight:(CGFloat)newValue {
145145
_primaryLineHeight = newValue;
146146
}
147147

148+
- (CGFloat)scaledPrimaryLineHeight {
149+
return [[UIFontMetrics defaultMetrics]
150+
scaledValueForValue:[self primaryLineHeight]];
151+
}
152+
148153
- (NSString *)primaryFontWeight {
149154
return _primaryFontWeight != nullptr
150155
? _primaryFontWeight

0 commit comments

Comments
 (0)