How to find the location of the cursor in a UITextView - iphone

I want to get the current position of cursor when I first click on a UITextView that contains some text.

I believe the UITextView selectedRange property should provide what you're after.

you could use UITextView's selectedRange to return the insertion point pf text.
The below code shows how to it.
NSString *contentsToAdd = #"some string";
NSRange cursorPosition = [tf selectedRange];
NSMutableString *tfContent = [[NSMutableString alloc] initWithString:[tf text]];
[tfContent insertString:contentsToAdd atIndex:cursorPosition.location];
[theTextField setText:tfContent];
[tfContent release];
for More to read the SO Post
Getting cursor position in a UITextView on the iPhone?
Also read the same on Apple discussion forum.
http://discussions.apple.com/thread.jspa?messageID=9940169

Related

How to set uitextfield to italics Xcode 4.5

I have an app where the users enters information in a few textfields which are then added to a uitextview by a nsstring does anybody know how i can set one of the textfields to italics and keep the others normal
regards
Edit:
Heres my code i want to only change once textfield (e.g textfbookpublisher):
NSString* combinedString = [NSString stringWithFormat:
#"%#,%#.'%#.'%#.%#,%#.",
textfbookpublisher.text,
textfbookauthor.text,
textfbooktitle.text,
textfbookplace.text,
nameofuni.text,
textfbookyear.text];
DissertationGen*bookg = [[DissertationGen alloc] init];
bookg.message = combinedString;
bookg.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:bookg animated:YES]
A quick search would have given you an easy answer. For example, this post.
Simply set the font of the UITextfield you want to a [UIFont italicSystemFontOfSize:].
try this link see accepted answer and also check TTTAttributedLabel library
Bold one Word
now you want only textfbookpublisher in italic then pass it individually to next DissertationGen view
in DissertationGen controller's viewdidload method use following code
UILabel *lblpubl = [[UILabel alloc] init];
lblpubl.font = [UIFont italicSystemFontOfSize:18.0];
lblpubl.text = your variable which contains textfbookpublisher's text;
now use lblpubl as you wish

how to insert text at any cursor position in uitextview?

i want to implement Code by which i can start to insert text at any position of cursor in UITextView in iphone sdk
any idea?
thank you in advance..
i refereed this link: iPhone SDK: How to create a UITextView that inserts text where you tap?
But not Getting it.
Dan's answer is manually changing the text. It's not playing well with UITextView's UndoManager.
Actually it's very easy to insert text with UITextInput protocol API, which is supported by UITextView and UITextField.
[textView replaceRange:textView.selectedTextRange withText:insertingString];
Note: It's selectedTextRange in UITextInput protocol, rather than selectedRange
This is what I use with a custom keyboard, seems to work ok, there may be a cleaner approach, not sure.
NSRange range = myTextView.selectedRange;
NSString * firstHalfString = [myTextView.text substringToIndex:range.location];
NSString * secondHalfString = [myTextView.text substringFromIndex: range.location];
myTextView.scrollEnabled = NO; // turn off scrolling
NSString * insertingString = [NSString stringWithFormat:#"your string value here"];
myTextView.text = [NSString stringWithFormat: #"%#%#%#",
firstHalfString,
insertingString,
secondHalfString];
range.location += [insertingString length];
myTextView.selectedRange = range;
myTextView.scrollEnabled = YES; // turn scrolling back on.
The simplest way (but it won't replace selected text) is to use the insertText: method:
[textView insertText:#"some text you want to insert"];
UITextView conforms to UITextInput which itself conforms to UIKeyInput.
Here is the Glorfindel's answer in Swift3. The text its inserting here it pulls out of the clipboard.
if let textRange = myTextView.selectedTextRange {
myTextView.replace(textRange, withText:UIPasteboard.general.string!)
}

iOS: Highlight spelling errors but disable suggestions?

My app implements a custom spell checker with its own window and a different workflow from the built-in spell checker in iOS. Therefore I have switched off correction in the main text input view. This disables the built-in suggestions, but also the highlighting of misspelled words.
Is there a way to keep the highlighting, but disable the suggestions?
You can use below code to check weather the printed word is correct spelled or not and if wrong then highlight that word
first import
#import <UIKit/UITextChecker.h>
in your file
-(BOOL)isDictionaryWord:(NSString*)word {
UITextChecker *checker = [[UITextChecker alloc] init];
NSLocale *currentLocale = [NSLocale currentLocale];
NSString *currentLanguage = [currentLocale objectForKey:NSLocaleLanguageCode];
NSRange searchRange = NSMakeRange(0, [word length]);
NSRange misspelledRange = [checker rangeOfMisspelledWordInString:word range: searchRange
startingAt:0 wrap:NO language: currentLanguage ];
return misspelledRange.location == NSNotFound;
}
You will have to roll your own. Here's a rough (iOS 5 and later):
Drop a view with transparent background on top of the text view.
Find the visible range of text like so:
- (NSRange)visibleRangeOfTextView:(UITextView *)textView {
CGRect bounds = textView.bounds;
UITextPosition *start = [textView characterRangeAtPoint:bounds.origin].start;
UITextPosition *end = [textView characterRangeAtPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))].end;
return NSMakeRange([textView offsetFromPosition:textView.beginningOfDocument toPosition:start],
[textView offsetFromPosition:start toPosition:end]);
}
Search for misspelled words in that range.
Find their on-screen coordinates using UITextView firstRectForRange method.
Highlight any way you want.
Deciding when to do this is left as an exercise for the eager student :)

UITextView insert text in the textview text

I want to have to occasionally insert text into the UITextView text object. For example, if the user presses the "New Paragraph" button I would like to insert a double newline instead of just the standard single newline.
How can I go about such? Do i have to read the string from UITextView, mutate it, and write it back? Then how would I know where the pointer was?
Thanks
Since the text property of UITextView is immutable, you have to create a new string and set the text property to it. NSString has an instance method (-stringByAppendingString:) for creating a new string by appending the argument to the receiver:
textView.text = [textView.text stringByAppendingString:#"\n\n"];
UITextview has an insertText method and it respects cursor position.
- (void)insertText:(NSString *)text
for example:
[myTextView insertText:#"\n\n"];
Here's how I implemented it and it seems to work nicely.
- (void) insertString: (NSString *) insertingString intoTextView: (UITextView *) textView
{
NSRange range = textView.selectedRange;
NSString * firstHalfString = [textView.text substringToIndex:range.location];
NSString * secondHalfString = [textView.text substringFromIndex: range.location];
textView.scrollEnabled = NO; // turn off scrolling or you'll get dizzy ... I promise
textView.text = [NSString stringWithFormat: #"%#%#%#",
firstHalfString,
insertingString,
secondHalfString];
range.location += [insertingString length];
textView.selectedRange = range;
textView.scrollEnabled = YES; // turn scrolling back on.
}
For Swift 3.0
You can append text by calling method insertText of UITextView Instance.
Example:
textView.insertText("yourText")
This is a correct answer!
The cursor position is also right.
A scroll position is also right.
- (void) insertString: (NSString *) insertingString intoTextView: (UITextView *) textView
{
[textView replaceRange:textView.selectedTextRange withText:insertingString];
}
- (void)insertStringAtCaret:(NSString*)string {
UITextView *textView = self.contentCell.textView;
NSRange selectedRange = textView.selectedRange;
UITextRange *textRange = [textView textRangeFromPosition:textView.selectedTextRange.start toPosition:textView.selectedTextRange.start];
[textView replaceRange:textRange withText:string];
[textView setSelectedRange:NSMakeRange(selectedRange.location + 1, 0)];
self.changesDetected = YES; // i analyze the undo manager in here to enabled/disable my undo/redo buttons
}

Getting cursor position in a UITextView on the iPhone?

We have a UITextView in our iPhone app which is editable. We need to insert some text at the cursor location when the users presses some toolbar buttons but can't seem to find a documented (or undocumented) method of finding the current location of the cursor.
Does anybody have any ideas or has anybody else achieved anything similar?
Like drewh said, you can use UITextView's selectedRange to return the insertion point. The length of this range is always zero. The example below shows how to it.
NSString *contentsToAdd = #"some string";
NSRange cursorPosition = [tf selectedRange];
NSMutableString *tfContent = [[NSMutableString alloc] initWithString:[tf text]];
[tfContent insertString:contentsToAdd atIndex:cursorPosition.location];
[theTextField setText:tfContent];
[tfContent release];
Swift 4:
// lets be safe, thus if-let
if let cursorPosition = textView.selectedTextRange?.start {
// cursorPosition is a UITextPosition object describing position in the text
// if you want to know its position in textView in points:
let caretPositionRect = textView.caretRect(for: cursorPosition)
}
We simply use textView.selectedTextRange to get selected text range and cursor position is at its start position.
Use UITextView selectedRange property to find the insertion point when the text view is first responder. Otherwise, when the view is not in focus, this property returns NSNotFound. If you need to know the cursor position in that case, consider subclassing UITextView and overriding canResignFirstResponder method, where you can store cursor position to a member variable.
Have you tried UITextView.selectedRange? It returns an NSRange, whose location element should tell you, where the cursor is.