How to get the range and Cursor Position in UITextView - ios

Hello I'm trying to create a custom text editor, for which I want to know the position of the cursor and range of the text in UITextView for which I can apply TextAttributes based on the user selected options.
For eg:
\n This is a the first paragraph string \n This is the second paragraph string \n
For this string, if the cursor is anywhere in the first paragraph string, and user select some text attribute, then I want to change only the This is a the first paragraph string based on selection.
For now, I know this can be achieved using multiple text attributes for specific range.I'm unable to get the range based on the cursor and break characters.
I'm new to this. If there is some other way to achieve this please let me know. Thanks

Related

Regarding Salesforce merge fields: How can I stop text from wrapping past the original length of the text box, when there is too many characters?

I have a few merge fields on a contract that I want to fit between other words in a paragraph. But when the information from Salesforce is too long for the text box it overlaps the following words in the contract, blocking the signers view of those words. I wanted to know if there is any functionality where the text continues but must be clicked on to view. Sort of like a cell in excel?
Thank You.
If you login to DocuSign and edit your merge field, you can set it to a fixed width under the 'Formatting' section on the right. This should lock the length of the text field, but it won't allow any more text into the field.

Array item completion in a UITextView

I'm trying to accomplish an autocomplete of my own for matching array items while typing. So, I already have the logic done for matching what's being typed with what's in the array, but my question is related to displaying the proposed correction in the UITextView like this screenshot
I was trying by splitting the text in the textview and replacing the last word into attributed strings, but that makes the proposed correction part of the actual contents. Any ideas of how this is accomplished in this app?
What you currently do is good , when you set the text of the textView to attributed string make bool flag say it's name is textEdited = true with a string part that the user types say it's name userStr , when textView change method is triggered check that bool and according to it make the search if it's true proceed search with userStr if it's not proceed search with whole textView text , don't forget to make textEdited= false after every zero suggested result
Edit: regarding the cursor put a label under the textfield with the same font as the textView and make it's background darkGray , also make background of the textview transparent and every attributed string assign it to the label so plus part of the label will be shown and cursor will be as it is in the textView

Read long paragraph of text from plist into a specific format with swift 3

I am currently writing my first app on ios using swift 3. I have a plist that has a list of different pieces of information and each is a long paragraph with sections throughout it. for each section, I want to bold the text for the title or at least have the option to format it a certain way rather than just display all the text. I currently have a simple table view that displays text in a text view once tapped. I cannot figure out how to read the paragraph into a string, and compare parts of the string and bold that specific text.
For example, If I had a string that was read from the plist and said:
"My name is #Bob and I like to #dance."
How could I change "#Bob" to "Bob" and "#dance" to "dance" without hard coding it?
#IBOutlet var paragraphTextView: UITextView!
.
.
.
if let text = paragraph["Text"] {
paragraphTextView.text = text
}
The simplest solution is to use some HTML markup in the text in your plist file.
Instead of the plain text:
My name is #Bob and I like to #dance.
Use:
My name is <b>Bob</b> and I like to <b>dance</b>.
This gives you far more flexibility. You can add bold, underline, and italic simply by using the appropriate <b></b>, <u></u>, and <i></i> tags as needed. It also allows you to markup more general ranges than single words.
Once the text has the proper markup, you create an NSAttributedString from the marked up string and then set that to the text viewsattributedTextproperty instead of using thetext` property.
For a good example of how to create an NSAttributedString from HTML text, see HTML Format in UITextView

iOS, textView half editable

I need help in string formatting in iOS UITextView. I have a textView, for now it`s editable. Now I want to add a few strings. they should be like header and user must be able to type only under it, and when he press ENTER or DELETE line sting should change their position, so at the end it should look like
Header 1(with color not editable)
/* user can type here
*/
Header 2 (with color not editable)
/* user can type here
*/
I think i need to use delegate and attributed string or something,
but I can`t figure it out. HELP please
The best way to achieve this is to make two labels and two text views on top of a scroll view. like
The heights of two text views can by dynamic depending on the text length. And in this case you can avoid endless IF conditions as you would need to put everything in a single Text view.
The advantage of this approach is the label text format will be very easy to set, you can simply set the attributed string in the storyboard.

Formatting a letter on textView in iOS

I have two pages long letter in textView. And all context of the letter should be shown in device's screen. I'm trying to achieve this by making the font smaller. The problem is that if I copy the letter into textView, it changes the font color to all black and format of the letter becomes weird.
I tried to edit the letter format in Storyboard but it's very hard. When I try to press "Enter" in textView, it doesn't put space between the lines but rather I come out from the textView.
Is textview the best place to put long context of the letter in? If so, how should format the letter in there?
To enter space use Option+Enter and if you are copying from text editor that support formatting then please remove all formatting then copy and paste it. to format text in textview use attributed string.
Why don't you format it in text editor and save it to a rtf file then, read the rtf into an attributed string, then set the attributedText of the text view? Like:
let textURL = NSBundle.mainBundle().URLForResource("MyLovelyLetter", withExtension: "rtf")
let options =[NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType]
let attribText = try! NSAttributedString(fileURL: textURL!, options: options, documentAttributes: nil)
textView.attributedText = attribText
for example

Resources