How to make NSMutableAttributedString work with emoji Swift 5


Hello guys, last week I got an issue related to NSMutableAttributedString. It couldn't work with emoji, when I entered three emoji like "🤣🤣🤣". It would only show "🤣🤣" not three emojies. So basically we need to handle about the length of emoji. Because it's different if you don't cast String to NSString.

Sometimes we use NSMutableAttributedString than String only because we want to customize our text like giving it a link and changing the color. Because when we use NSMutableAttributedString, we can give the business logic how it will show in view controller. And it's easy to work with TextView. We only can do "textView.attributedText = NSMutableAttributedString". And it will automatically run like we set in NSMutableAttributedString.

If you don't work NSMutableAttributedString, you would not counter this issue. Because I have tried not to use NSMutableAttributedString in TextView. It just works fine. The solution for the issue is very simple you just need to cast String to NString. Why you need to cast it to NString because the length between String and NString are different when they work with emoji.

That's is. You have to notice casting to NSString and get the right length of the text (string as NSString).length. If you do this, I'm pretty sure. You can work with emoji, I have tested it :D. By the way, I found the solution on stackoverflow but my previous solution is to use String rathan than NSMutableAttributedString. I tested it first in my playground.

If you have any question, please feel free to ask. I'm ready to help, thanks for visiting my simple blog, I hope you can learn a lot reading our articles.



Popular posts from this blog

How to restart the app with flutter Android and iOS

How to have ConstraintLayout inside ScrollView and ScrollView inside ConstraintLayout Android

Missing system image Android Studio solution