How to create ruby text (Japanese furigana) with SwiftUI? - ios

I am trying to build an iOS app with Japanese text and I want to display some Furigana over my Kanji. Meaning I need to show ruby text.
I am using SwiftUI and have absolutely zero knowledge of UIKit.
I have read that there are some ways to make this possible in UIKit, however I am still a rookie that started with SwiftUI and still have a lot to learn.
So my question is: is it even possible in SwiftUI? If not, is there a way to connect something from UIKit to make this work?
Thanks a lot in advance! Have a wonderful day :)

You can use an NSAttributedString with kCTRubyAnnotationAttributeName to "add" the furigana on top of the kanji. There are lots of answers on StackOverflow about this. Here is one: https://stackoverflow.com/a/54294243/532368
The problem is that SwiftUI Text won't work with NSAttributedString, so you need to create a UIViewRepresentable that uses a UILabel to display the attributed string with the ruby annotation correctly. You can find an example (and explanations) in the Attributed Strings with SwiftUI article by The SwiftUI Lab.
That should give the desired result and allow you to display the furigana on top of the kanji.
Update: I haven't tried, but it seems the ApolloZhu/RubyAttribute
project on GitHub provides the solution you need as a library.

Related

How to highlight text in a textView in Swift?

I would like to highlight a part of text in a UITextView like Pages for iOS or Safari for iOS do. I search the documentation but I can't find a way to do it, maybe because I'm a beginner in iOS development and I miss some crucial info. Somebody can help me?
Screenshot of Pages for iOS:
Screenshot of Safari for iOS:
If you only need to highlight a single, contiguous block of text you can set the selection programmatically. Your examples show multiple discontinuous parts however. For that you are going to need to use an attributed string. There is no other system-provided highlighting function. (There might be a third party library that offers this though - you'll have to search.)

Changing font of all UI elements using Text Kit

One of the most common problem faced by iOS developers is to change the font of all the UI elements in one go.
With the introduction of UIAppearance Protocol setting the font did become very convenient, since then I've been using appearance to achieve this.
I was going through this tutorial trying to understand Text Kit introduced in iOS 7. The tutorial uses subclass of NSTextStorage to beautifully format the text.
Now I was just wondering if it is possible to change the font of UI elements by subclassing the NSTextStorage. I spend few hours trying to figure out where to start from but couldn't achieve anything so far. I appreciate if any one can give me a hint to where to start from and some tips to get this done.
I welcome all your suggestions and views on this.

How to make Facebook mention like highlighting word in UITextView?

HERE IS SAMPLE IMAGE -->
I'm trying to make text input feature very similar to Facebook's one. The mention that start with # will generate list of my friends name then selecting one will act as something like an object. So deleting the highlighted word will remove whole word in UITextView.
I wonder if that is part of UITextView. Does anyone know how to implement?
Thank you in advance.
I know this is a pretty old question but I've actually made libraries that assist with mentions, when to show the list, does the highlighting etc. It uses attributes on an attributed string to set background.
https://github.com/szweier/SZMentions
https://github.com/szweier/SZMentionsSwift
You'll want to have a look at NSAttributedString. While this class exists in iOS 3.2 or greater, you cannot display it in a UITextView unless you are on iOS 6 or later.

How do I go about building a native iOS rich text editor?

We want a native rich text editor because we are trying to stay away from using Javascript and webviews for this solution.
We've tried many things so far, and we're left with quite a few obstacles that we just can't get around. Let me break it down into questions that I hope you can answer.
I have a UIButton, that says "B" on it, and I want to put it into the 'selected' state when a user sets 'Bold' from using the TextView's long-press gesture on a selection. How do I register for this state change? I tried adding an observer on the textView.attributedText, but it doesn't seem they are changing that dictionary, but instead are updating it. NSDictionary has no way, as far as I know, to add observers on the dictionary's keys. So I'm stuck with noticing this change.
Regarding number 1, I also tried setting the textView's inputDelegate and it seems that the method - (void)textDidChange:(id )textInput never gets called. :( Docs says is should. What did I do wrong?
How do I update the attributedText weight when I hit my bold italic or underline button.
How do I convert my attributed text into HTML?
I saw a few neat libraries for number 4, but I'm still curious what you'd come up with. (Broadens my options). But, I can't really work on number 4 until I figure out how to do the previous 3.
This editor will also need hyperlinks, bulleted lists, and numbered lists, more things I imagine I'll struggle through, but if you could answer the 4 questions above, that will keep me held over for a while. :)
Thanks!
Here is a link to an iOS rich text editor I've been working on.
https://github.com/aryaxt/iOS-Rich-Text-Editor
There is still a lot of work that has to be done, but the basic features are there.
The Apple sample application called 'TextEdit' does much of what you've described and, if not that, would be a very good starting point. Find the sample code with a search in the Organizer.
There is a commercial editor based on the DTCoreText library. I've used that library but not the rich text editor. Look at the Cocoanetics web site. It's not cheap but will save you a ton of work.

Simple rich text view in cocoa-touch

I'm working on an iOS app in which I need to implement a basic text view with these requirements:
Rich text
Font emphasis (bold, italics).
Inline images for hyperlink-like actions.
Scrolling
Getting and setting the scroll offset (for remembering the previous scroll position).
Text selection
Getting the selected text character range.
Scrolling the view when selecting text, if needed.
I believe I could achieve this with UIWebView but the problem with this is that it provides very little control and is somewhat slow and shows a blank screen while loading. I was wondering if this could be achieved with Core Text but regarding the text selection I'm not sure. I'm hoping to achieve as iOS-native behaviour as possible.
What I'd need is pretty much like Instapaper's text view.
OmniGroup have done something like this by creating a text editor much like UITextView which draws the text using Core Text but also has all the editing features of UITextView as well as Rich text features from Core Text.
You can find it here.
There are some answers about this in StackOverflow, for example: Core text tutorial
However the best tutorial I ever read about Core Text in iOS is not listed in any answer I've found: How to create a simple magazine app with core text
Please notice that CoreText is only available for iOS 3.2 or superior.

Resources