This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UITextView with Syntax Highlighting
I'm working on a source code editor for the iPad, but I'm stuck on square one: syntax highlighting. I'm planning on using libclang to do the heavy lifting, but there doesn't seem to be a good way to show rich text on iOS. I think the best solution would be to have a subclass of UITextView that supports NSAttributedString drawing, but I'm not sure how to do that. I've seen things like Omni's text editor, but it doesn't look very good. I only need multiple colors of a monospaced font. Is there a framework or library that would help draw attributed strings in a UITextView subclass?
Thanks in advance.
http://www.cocoanetics.com/parts/dtrichtexteditor/ is a paid solution that would do almost all the work for you.
https://github.com/Cocoanetics/DTCoreText is the open source backend for just the text highlighting.
Related
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.
This question already has answers here:
How to add toggleBoldface option to a UITextView in iOS?
(2 answers)
Closed 5 years ago.
I have been searching for quite a while for a solution as to how to grant users the ability to underline, italicize and bold text in a UITextView but have not been able to find a solid solution. I have realized that I will need to use an NSAttributedString but am not sure as to how to create the UI and format the string so I am able to save it to a database. If you could point me in a proper direction that would be greatly appreciated. Thanks!
The UI is simply an ordinary editable UITextView with allowsEditingTextAttributes set to true.
How you save is up to you, but the most universal solution is probably to pull out the UITextView's attributedText, archive it into an NSData (now known as Data in Swift), and save it off.
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.)
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.
I want to style a label. Nothing crazy, just some coloring, changing of font-sizes for specific words, etc. I can do this with NSAttributedString, but would it be possible/better to do in TextKit, newly introduced with iOS 7?
I've looked around, but TextKit doesn't yet have a ton of tutorials, and none seem to address this.
It should still be done with NSAttributedStrings as the parameters you've listed exactly describe the attributes of strings. NSTextStorage is a subclass of NSMutableAttributedString but it's not intended for what you want to do (I think this class might have misled you).
Using Text Kit to Draw and Manage Text contains a nice overview of Text Kit objects, I highly recommend reading it.