Skip to content

Commit 8543782

Browse files
authored
Merge pull request #3 from software-mansion-labs/@hryhoriiK97/use-consistent-styling-on-both-platforms
[iOS/Android] Use consistent styling on both platforms
2 parents 032a06b + ac51a8a commit 8543782

11 files changed

Lines changed: 93 additions & 186 deletions

example/src/App.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ import {
66
Linking,
77
} from 'react-native';
88
import { RichTextView } from 'react-native-rich-text';
9-
import type { HeaderConfig } from 'react-native-rich-text';
10-
11-
const HEADER_CONFIG: HeaderConfig = {
12-
scale: 2.0,
13-
isBold: true,
14-
};
159

1610
const sampleMarkdown = `#### Welcome to the React Native Markdown component!
1711
@@ -49,7 +43,6 @@ export default function App() {
4943
style={styles.markdown}
5044
fontSize={18}
5145
fontFamily="Helvetica"
52-
headerConfig={HEADER_CONFIG}
5346
color="#F54927"
5447
onLinkPress={handleLinkPress}
5548
/>

ios/RichTextView.mm

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
#import "MarkdownASTNode.h"
44
#import "AttributedRenderer.h"
55
#import "RenderContext.h"
6-
#import "RichTextTheme.h"
76

87
#import <react/renderer/components/RichTextViewSpec/ComponentDescriptors.h>
98
#import <react/renderer/components/RichTextViewSpec/EventEmitters.h>
109
#import <react/renderer/components/RichTextViewSpec/Props.h>
1110
#import <react/renderer/components/RichTextViewSpec/RCTComponentViewHelpers.h>
1211

1312
#import "RCTFabricComponentsPlugins.h"
13+
#import <React/RCTConversions.h>
14+
#import <React/RCTFont.h>
1415

1516
using namespace facebook::react;
1617

@@ -24,7 +25,7 @@ - (void)setupTextView;
2425
- (void)setupConstraints;
2526
- (void)renderMarkdownContent:(NSString *)markdownString withProps:(const RichTextViewProps &)props;
2627
- (void)textTapped:(UITapGestureRecognizer *)recognizer;
27-
- (UIFont *)createFontWithFamily:(NSString *)fontFamily size:(CGFloat)size;
28+
- (UIFont *)createFontWithFamily:(NSString *)fontFamily size:(CGFloat)size weight:(NSString *)weight style:(NSString *)style;
2829
@end
2930

3031
@implementation RichTextView {
@@ -99,22 +100,22 @@ - (void)renderMarkdownContent:(NSString *)markdownString withProps:(const RichTe
99100
}
100101

101102
AttributedRenderer *renderer = [AttributedRenderer new];
102-
RichTextTheme *theme = [RichTextTheme defaultTheme];
103103

104104
CGFloat fontSize = props.fontSize > 0 ? props.fontSize : kDefaultFontSize;
105105

106-
// Create font with family and size
107106
NSString *fontFamily = [[NSString alloc] initWithUTF8String:props.fontFamily.c_str()];
108-
theme.baseFont = [self createFontWithFamily:fontFamily size:fontSize];
109-
if (_textView.textColor) { theme.textColor = _textView.textColor; }
107+
NSString *fontWeight = [[NSString alloc] initWithUTF8String:props.fontWeight.c_str()];
108+
NSString *fontStyle = [[NSString alloc] initWithUTF8String:props.fontStyle.c_str()];
109+
UIFont *font = [self createFontWithFamily:fontFamily size:fontSize weight:fontWeight style:fontStyle];
110110

111-
const auto &headerConfig = props.headerConfig;
112-
113-
theme.headerConfig.scale = headerConfig.scale > 0 ? headerConfig.scale : 2.0;
114-
theme.headerConfig.isBold = headerConfig.isBold;
111+
// Get color from props or use textView's current color
112+
UIColor *color = _textView.textColor ?: [UIColor blackColor];
113+
if (props.color) {
114+
color = RCTUIColorFromSharedColor(props.color);
115+
}
115116

116117
RenderContext *renderContext = [RenderContext new];
117-
NSMutableAttributedString *attributedText = [renderer renderRoot:ast theme:theme context:renderContext];
118+
NSMutableAttributedString *attributedText = [renderer renderRoot:ast font:font color:color context:renderContext];
118119

119120
// Add custom attributes for links
120121
for (NSUInteger i = 0; i < renderContext.linkRanges.count; i++) {
@@ -144,23 +145,25 @@ - (void)updateProps:(Props::Shared const &)props
144145
needsRerender = YES;
145146
}
146147

147-
if (oldViewProps.textColor != newViewProps.textColor) {
148-
NSString *textColorString = [[NSString alloc] initWithUTF8String:newViewProps.textColor.c_str()];
149-
_textView.textColor = [self hexStringToColor:textColorString];
148+
if (oldViewProps.color != newViewProps.color) {
149+
// Convert React Native SharedColor to UIColor
150+
UIColor *color = RCTUIColorFromSharedColor(newViewProps.color);
151+
if (color) {
152+
_textView.textColor = color;
153+
}
150154
needsRerender = YES;
151155
}
152156

153-
if (oldViewProps.fontSize != newViewProps.fontSize || oldViewProps.fontFamily != newViewProps.fontFamily) {
157+
if (oldViewProps.fontSize != newViewProps.fontSize || oldViewProps.fontFamily != newViewProps.fontFamily || oldViewProps.fontWeight != newViewProps.fontWeight || oldViewProps.fontStyle != newViewProps.fontStyle) {
154158
CGFloat fontSize = newViewProps.fontSize > 0 ? newViewProps.fontSize : kDefaultFontSize;
155159
NSString *fontFamily = [[NSString alloc] initWithUTF8String:newViewProps.fontFamily.c_str()];
156-
_textView.font = [self createFontWithFamily:fontFamily size:fontSize];
160+
NSString *fontWeight = [[NSString alloc] initWithUTF8String:newViewProps.fontWeight.c_str()];
161+
NSString *fontStyle = [[NSString alloc] initWithUTF8String:newViewProps.fontStyle.c_str()];
162+
_textView.font = [self createFontWithFamily:fontFamily size:fontSize weight:fontWeight style:fontStyle];
157163
needsRerender = YES;
158164
}
159165

160-
if (oldViewProps.headerConfig.scale != newViewProps.headerConfig.scale ||
161-
oldViewProps.headerConfig.isBold != newViewProps.headerConfig.isBold) {
162-
needsRerender = YES;
163-
}
166+
// HeaderConfig removed - no longer needed
164167

165168
if (needsRerender && !newViewProps.markdown.empty()) {
166169
NSString *markdownString = [[NSString alloc] initWithUTF8String:newViewProps.markdown.c_str()];
@@ -175,22 +178,6 @@ - (void)updateProps:(Props::Shared const &)props
175178
return RichTextView.class;
176179
}
177180

178-
- (UIColor *)hexStringToColor:(NSString *)hexString {
179-
if (!hexString.length) return nil;
180-
181-
NSString *cleanHex = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
182-
if (cleanHex.length != 6) return nil;
183-
184-
NSScanner *scanner = [NSScanner scannerWithString:cleanHex];
185-
unsigned hexValue;
186-
if (![scanner scanHexInt:&hexValue]) return nil;
187-
188-
CGFloat red = ((hexValue >> 16) & 0xFF) / 255.0f;
189-
CGFloat green = ((hexValue >> 8) & 0xFF) / 255.0f;
190-
CGFloat blue = (hexValue & 0xFF) / 255.0f;
191-
192-
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
193-
}
194181

195182
// MARK: - Gesture handling
196183

@@ -269,15 +256,23 @@ - (void)textTapped:(UITapGestureRecognizer *)recognizer {
269256

270257
// MARK: - Helper methods
271258

272-
- (UIFont *)createFontWithFamily:(NSString *)fontFamily size:(CGFloat)size {
273-
if (fontFamily && fontFamily.length > 0) {
274-
UIFont *customFont = [UIFont fontWithName:fontFamily size:size];
275-
if (customFont) {
276-
return customFont;
277-
}
259+
- (UIFont *)createFontWithFamily:(NSString *)fontFamily size:(CGFloat)size weight:(NSString *)weight style:(NSString *)style {
260+
// Use React Native's RCTFont.updateFont for consistent font handling
261+
NSString *fontWeight = weight && weight.length > 0 ? weight : nullptr;
262+
NSString *fontStyle = style && style.length > 0 ? style : nullptr;
263+
264+
// Handle edge case: weight "0" should be treated as nullptr
265+
if ([fontWeight isEqualToString:@"0"]) {
266+
fontWeight = nullptr;
278267
}
279268

280-
return [UIFont systemFontOfSize:size];
269+
return [RCTFont updateFont:nullptr
270+
withFamily:fontFamily
271+
size:@(size)
272+
weight:fontWeight
273+
style:fontStyle
274+
variant:nullptr
275+
scaleMultiplier:1];
281276
}
282277

283278
@end

ios/renderer/AttributedRenderer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#import <Foundation/Foundation.h>
22

33
@class MarkdownASTNode;
4-
@class RichTextTheme;
54
@class RenderContext;
65

76
@interface AttributedRenderer : NSObject
87
- (NSMutableAttributedString *)renderRoot:(MarkdownASTNode *)root
9-
theme:(RichTextTheme *)theme
8+
font:(UIFont *)font
9+
color:(UIColor *)color
1010
context:(RenderContext *)context;
1111
@end
1212

0 commit comments

Comments
 (0)