Skip to content

Fix capitalize Text style on IOS#32774

Closed
MaeIg wants to merge 4 commits into
react:mainfrom
MaeIg:fix-capitalize-on-ios
Closed

Fix capitalize Text style on IOS#32774
MaeIg wants to merge 4 commits into
react:mainfrom
MaeIg:fix-capitalize-on-ios

Conversation

@MaeIg

@MaeIg MaeIg commented Dec 16, 2021

Copy link
Copy Markdown
Contributor

Summary

On my project, I realized that capitalize style doesn't work with dates on IOS. I found this issue and tried to solve it.

(code example: https://snack.expo.dev/@maelg/capitalize-demo)

Android IOS Web
image image image

As we can see, the behavior is different on IOS, Android and web:

  • Android: Capitalize the first letter of each word, unless it begins with a number. And put the rest in lowercase.
  • IOS: Capitalize the first letter of each word, unless it begins with a number. And put the rest in lowercase.
  • Web: Capitalize the first letter of each word, unless it begins with a number. And put the rest in lowercase.

This PR aims to unify behavior on Android and Ios. I am not changing the behavior which differs from the web because I don't know if it is desirable to align with the web.

Changelog

[IOS] [Changed] - Don't capitalize the first letter of a word that is starting by a number

Test Plan

I manually tested my changes on a POC app (same code: https://snack.expo.dev/@maelg/capitalize-demo) on react-native v0.66.4 and react-native main branch.

You can see the result here:

Android IOS
image image

I tried to use rn-tester but it was not taking my code. I think it is because fabric was enabled so it was using other code.
I tried to disable fabric but I was not able to build the app on my IOS simulator anymore:

On rn-tester:

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 16, 2021
@MaeIg MaeIg changed the title Fix capitalize on IOS Fix capitalize Text style on IOS Dec 16, 2021
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Dec 16, 2021
@react-native-bot react-native-bot added the Platform: iOS iOS applications. label Dec 16, 2021
@analysis-bot

analysis-bot commented Dec 16, 2021

Copy link
Copy Markdown
Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: 3bd1159
Branch: main

@analysis-bot

analysis-bot commented Dec 16, 2021

Copy link
Copy Markdown
Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 8,293,521 +5,943
android hermes armeabi-v7a 7,624,024 +4,342
android hermes x86 8,764,166 +6,221
android hermes x86_64 8,703,118 +6,758
android jsc arm64-v8a 9,681,919 +5,893
android jsc armeabi-v7a 8,669,110 +4,292
android jsc x86 9,638,068 +6,157
android jsc x86_64 10,234,605 +6,681

Base commit: 3bd1159
Branch: main

@yungsters

Copy link
Copy Markdown
Contributor

Thanks for the contribution! It is interesting that web preserves pre-existing capitalized letters. I agree that we do not need to consider that as part of this PR, but it is interesting to consider which behavior is desirable longer term.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@yungsters has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@sota000

sota000 commented Dec 16, 2021

Copy link
Copy Markdown
Contributor

Hi there! For testing it with RN-tester, can you please try removing Pods folder, Podfile.lock, and build folder before running pod install with fabric_enabled off?

@MaeIg MaeIg force-pushed the fix-capitalize-on-ios branch from 118e09e to 2a7e0ef Compare December 16, 2021 22:11
@MaeIg

MaeIg commented Dec 16, 2021

Copy link
Copy Markdown
Contributor Author

Thanks for your quick answers!

I managed to launch rn-tester without fabric thanks to you @sota000! I'll edit my first comment to add screenshots.
I found a crash on rn-tester when there are several spaces in a row. I just pushed a fix!

Comment thread Libraries/Text/RCTTextAttributes.m Outdated
Comment on lines 243 to 244

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we prefer to use new if there's no initializer params

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I didn't know this syntax. Done!

Comment on lines 801 to 806

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add one for 2 digit dates as well

@MaeIg MaeIg Dec 19, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!
It still works:

Comment thread Libraries/Text/RCTTextAttributes.m Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why aren't we just joining on newWords? NSString's description is just the string itself

@MaeIg MaeIg Dec 19, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you're right. Fixed!

Comment thread Libraries/Text/RCTTextAttributes.m Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this a C function? i.e.

static NSString *CapitalizeText(NSString *text) {
  ...
}

this is more lightweight and the function does not require any state from this class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good. Tell me if it isn't!

As you can see with the code I wrote, I'm not familiar with Objective-C. I learned while I was making this PR 😁 So, thanks a lot for taking the time to review this PR, I learned a lot!

It would crash the app if there were several spaces in a row
Also rename the function to be clearer
@MaeIg MaeIg force-pushed the fix-capitalize-on-ios branch from 2a7e0ef to 3fb165a Compare December 19, 2021 17:32
@MaeIg MaeIg requested a review from philIip December 19, 2021 17:40
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@yungsters has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @MaeIg in 8b5a5d4.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label Jan 25, 2022
@MaeIg MaeIg deleted the fix-capitalize-on-ios branch January 27, 2022 09:40
facebook-github-bot pushed a commit that referenced this pull request Apr 22, 2022
Summary:
<!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? -->

Few month ago, I created a [pull request](#32774) to unify the behavior of capitalize style between Android and IOS.

But, I found out that it doesn't work when fabric is enabled. We have the old behavior :
| Android | IOS |
| ------------- | ------------- |
| <img width="458" alt="capitalize_android_fabric" src="https://user-images.githubusercontent.com/40902940/163182082-4061003c-230b-46f7-9e93-c34b66dbf3d2.png"> | <img width="476" alt="capitalize_ios_fabric" src="https://user-images.githubusercontent.com/40902940/163182124-b6dee450-46e3-41a3-b5bb-553d7c2662e6.png"> |
(source: rn-tester, last commit: dac56ce)

As fabric is now live since v0.68, we should fix this behavior for fabric aswell.

I don't know why there is so much duplicated code between `ReactCommon/react/renderer/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm` and `Libraries/Text/RCTTextAttributes.m` because I don't know the architecture of the project very well. But if you see missing tests or some refacto to do I'm open to suggestions!

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[iOS] [Fixed] - Don't capitalize the first letter of a word that is starting by a number (Fabric renderer)

Pull Request resolved: #33629

Test Plan:
I manually tested these changes on rn-tester (react-native `main` branch):
| Fabric ? | Android | IOS |
| ------------- | ------------- | ------------- |
| YES | <img width="458" alt="capitalize_android_fabric" src="https://user-images.githubusercontent.com/40902940/163182082-4061003c-230b-46f7-9e93-c34b66dbf3d2.png"> | <img width="476" alt="capitalize_ios_fabric_after" src="https://user-images.githubusercontent.com/40902940/163184277-6c58c117-7144-4f6b-98ea-0c1db654f27b.png"> |
| NO | <img width="458" alt="android_nf_after" src="https://user-images.githubusercontent.com/40902940/163190263-9d801f6a-09c2-4ec6-a841-3dca115a5ef7.png"> | <img width="476" alt="ios_nf_after" src="https://user-images.githubusercontent.com/40902940/163190333-7e9eac6a-3f28-4826-8ef9-bcf45bf870a9.png"> |

Reviewed By: cortinico

Differential Revision: D35611086

Pulled By: GijsWeterings

fbshipit-source-id: 0c43807dcddb30e65921eb1525c0fe440162ec32
Saadnajmi pushed a commit to Saadnajmi/react-native-macos that referenced this pull request Jan 15, 2023
…33629)

Summary:
<!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? -->

Few month ago, I created a [pull request](react#32774) to unify the behavior of capitalize style between Android and IOS.

But, I found out that it doesn't work when fabric is enabled. We have the old behavior :
| Android | IOS |
| ------------- | ------------- |
| <img width="458" alt="capitalize_android_fabric" src="https://user-images.githubusercontent.com/40902940/163182082-4061003c-230b-46f7-9e93-c34b66dbf3d2.png"> | <img width="476" alt="capitalize_ios_fabric" src="https://user-images.githubusercontent.com/40902940/163182124-b6dee450-46e3-41a3-b5bb-553d7c2662e6.png"> |
(source: rn-tester, last commit: dac56ce)

As fabric is now live since v0.68, we should fix this behavior for fabric aswell.

I don't know why there is so much duplicated code between `ReactCommon/react/renderer/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm` and `Libraries/Text/RCTTextAttributes.m` because I don't know the architecture of the project very well. But if you see missing tests or some refacto to do I'm open to suggestions!

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[iOS] [Fixed] - Don't capitalize the first letter of a word that is starting by a number (Fabric renderer)

Pull Request resolved: react#33629

Test Plan:
I manually tested these changes on rn-tester (react-native `main` branch):
| Fabric ? | Android | IOS |
| ------------- | ------------- | ------------- |
| YES | <img width="458" alt="capitalize_android_fabric" src="https://user-images.githubusercontent.com/40902940/163182082-4061003c-230b-46f7-9e93-c34b66dbf3d2.png"> | <img width="476" alt="capitalize_ios_fabric_after" src="https://user-images.githubusercontent.com/40902940/163184277-6c58c117-7144-4f6b-98ea-0c1db654f27b.png"> |
| NO | <img width="458" alt="android_nf_after" src="https://user-images.githubusercontent.com/40902940/163190263-9d801f6a-09c2-4ec6-a841-3dca115a5ef7.png"> | <img width="476" alt="ios_nf_after" src="https://user-images.githubusercontent.com/40902940/163190333-7e9eac6a-3f28-4826-8ef9-bcf45bf870a9.png"> |

Reviewed By: cortinico

Differential Revision: D35611086

Pulled By: GijsWeterings

fbshipit-source-id: 0c43807dcddb30e65921eb1525c0fe440162ec32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Platform: iOS iOS applications. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants