Craig
Craig

Reputation: 1085

When the enter key is pressed on the keyboard display some text

This is what I want the app to do.

Tap in a "Text View" not a "Text Field", then the keyboard is displayed and a bullet point ("\u2022") pops up. You type your sentence, and when you hit return it makes a new bullet.

So essentially, you have a Text View that is a bulleted list. Much like Word, where you start a bullet and when you hit return it make a new bullet for you, so you can start typing on that line.

what I want my app to do

How do I do this?

This is what worked for any other stupid noobs like me:

I put this into my .m file

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range  replacementText:(NSString *)text
{
if([text isEqualToString:@"\n"])
{
NSString *modifiedString = [myTextView.text stringByAppendingString:@"\n\u2022"];
[myTextView setText:modifiedString];
return NO;
}
return YES;
}

I put this into my .h file

@interface CRHViewController3 : UIViewController <UITextViewDelegate> {

    __weak IBOutlet UITextView *myTextView; 


} 

and then I put this under my viewDidLoad

    - (void)viewDidLoad
{
    [super viewDidLoad];

[myTextView setDelegate:self];   

}

Upvotes: 1

Views: 1250

Answers (3)

Regan
Regan

Reputation: 1497

To use this code:

(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if([text isEqualToString:"\n"])
    {
        NSString *modifiedString = [textView.text stringByAppendingString:@"\n\u2022"];
        [textView setText:modifiedString];
        return NO;
    }
    return YES;
}

Your header file must look like this:

@interface MyViewController : UIViewController <UITextViewDelegate> {

   //instance variables in here
}

Upvotes: 0

AshwinN
AshwinN

Reputation: 301

The method -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text would be called for each letter entered on deleted. So in this method you could try to replace all line breaks with bullet char.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
...
   textView.text = [textView.text stringByReplacingOccurrencesOfString:@"\\n" withString:@"\u2022"];
...
}

Upvotes: 0

Sj.
Sj.

Reputation: 1724

Try out this.

(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if([text isEqualToString:"\n"])
    {
        NSString *modifiedString = [textView.text stringByAppendingString:@"\n\u2022"];
        [textView setText:modifiedString];
        return NO;
    }
    return YES;
}

Upvotes: 2

Related Questions