how to highlight text in uitextview in iphone - iphone

Can any one help me how to highlight text in a UITextView. The text is coming from sqlite. I have implemented a search with UISearchBar. I need to highlight texts related the search result.

The only possible solution is use Core Text Framework. It is very powerful but it require some knowledge to use.
There is no other way to do this.

There is other way for iOS 7 and up. It's by using TextKit framework.
Simple tutorial with examples can be found here: http://www.objc.io/issue-5/getting-to-know-textkit.html

Related

Best approach to displaying text

Xcode 8.3.2
As as newbie, I'm trying to get acquainted with segues, constraints, text fields and Image Views...mainly the design elements. I'm trying to put together a cheesy recipe book of favorite recipes. I wanted to know what's the best approach to display rich text with bold text and bullet points? Labels seem to be my only option but I was wondering if there were other options? Any advice would be appreciated.
ViewController in Storyboard
As much for my approach, during my career I learned that textViews (aka C# RichTextBoxes) can be pretty tricky..
You can go with classical UILabel approach with lines in Interface Builder set to 0 and atrributedText, which contains bullet points:
This is a nice tutorial, but a bit old and would probably do it another way, but I guess for the purposes is fine...
https://wingoodharry.wordpress.com/2016/04/10/bullet-point-list-ios-swift/
I would like to state here, that TextViews are the last option everywhere with text...
Wish happy coding! :)
UILabels are best way to display text, you can use attributedText in UILabels which will enhance your UI. Multiline text is a add on.
Else try using AsyncDisplayKit.

How to keep 2 different fonts within the same UITextField or UITextView?

I want to set 2 different fonts within the same UITextField and UITextView . How to do it?
Its a bit of work - you'll need to use Core Text and NSAttributedString to do this.
There are plenty of tutorials and examples, although I'd suggest using someone else's already-made UILabel subclass such as:
OHAttributedLabel
or
TTAttributedLabel
As these usually have some convenience methods to make handling a lot easier.
I would do it with 2 custom textfields overlaying, both backgroundcolor:clearColor, maybe stuffed on an image that represents the background.
I don't think it is possible to handle 2 different fonts within the same UITextField or UITextView. If you want to have different font style you can either set different font style within a UIWebView or use the coreText API.
Here are some links that might help:
iPhone Development - Setting UIWebView font
the official doc on core text: https://developer.apple.com/library/mac/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html
I know you already picked a valid answer but... don't do it that way... it's not worth it. Use a webview instead and draw everything with html.
In interface builder change Text View's Text field to Attributed. In that small editor that appears you can change the font/format/color of the selected text, like in any advanced text editor.

printing text as a paragraph(indentation) in uitextview in iphone

A basic functionality I am trying to implement in UITextView.
I have a text (NSString) and I am trying to place in to the text field as a paragraph (indentation). Is it possible? I have seen many links but none of them have an exact answer.
We have a property named textAlignment but it is for left, right or center not indention.
Can anyone suggest how to implement this?
You should consider using a UIWebView if your displayed text has formatting. Using textViews and labels quickly renders the code to complex.
Possible solution in this post - as the first answer suggested, use a UIWebView.

Is UITextView enough for a simple writing app?

I'm trying to make a simple writing program, like Notes app for iPad, with a custom keyboard.
From OmniGroup's text editor open sources, I learned that UITextInput is mainly required to develop great text editor capable of having several fonts and size in one editor.
But it seemed tough to implement a lot of methods in UITextInput Protocol in order to build a text editor from scratch.
Do you think UITextView is enough to make a simple writing program?
Do most writing apps in AppStore use their own framework using UITextInput?
Thank you!
UITextView is the ideal view to display and edit multiple lines of text. You can format the text using font, size and color. The only formatting restriction is that you can have only one text format for the whole view. The Notes app written by Apple is a typically use of UITextView.
Check Apple documentation for more...
Cheers

Is CoreText just a draw/layout Framework for text?

Im using CoreText to display some text, creating framesetter, frames and so, and everything is fine. I can even format the text, but this all is done before I draw. Now the question that is driving me crazy:
CoreText is just to render text? I cannot get any reference to CTRuns or Glyphs to highlight them?
Another sub big question, the Pages App dont use CoreText, anyone knows? In Pages, you can select any already drawn styled text!
All I want is to draw the text and be able to let the user tap to select any text or doubleTap to select the paragraph.
Please, anyone, any light?
Thanks in advance.
CoreText is just to render text. Implementing user interaction is quite another matter. You need to implement UITextInput protocol, see here. It's a big job.
If you just want to input text and don't have a need for advanced typography, just use UITextView or UIWebView.
you can get image bounds of CTLines and CRRuns via core text here is little example i made for my app it's not perfect yet but you can get a little idea of how it works.
textViewProject