Run TTS in NSArray sequentially - objective-c

I have NSArray with words in it.
I want to app reads that tts sequentially and print the text in UILabel what app speak.
but word on label is shown the last one.
I tried time pause, etc
for (Word * w in ttswords) {
[self speechword:w];
}
-(void)speechword:(Word*)w{
utterance = [[AVSpeechUtterance alloc] initWithString:[w.title stringByReplacingOccurrencesOfString:#"~" withString:#""]];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:#"en-US"];
[synthesizer speakUtterance:utterance];
_lb_title.text = w.title;
}
I want to execute one by one.

The best way to highlight the vocalized word is using the speechSynthesizer:willSpeakRangeOfSpeechString:utterance: method of the AVSpeechSynthesizerDelegate protocol.
If you don't use this delegate method, you won't be able to reach your goal.
Take a look at this complete and useful example (ObjC and Swift) that displays each vocalized word in a bold font with the speech synthesis.

Related

Resetting the search query string in UISearchController programmatically

I am failing to reset/substitute the search query string programmatically. I was trying to modify the UISearchBar query string from within the UISearchControllerDelegate.
I am using dictation input.
When the command "delete" is said, the searchController.searchBar.text should be set back to #"". The speech detection should continue as normal with the empty string.
The query string does get reset to #"", but the speech detection stops.
How can I reset search query string in UISearchController programmatically and still be able to continue the input using speech?
- (void)updateSearchResultsForSearchController:(nonnull UISearchController *)searchController {
NSString *searchQuery = self.searchController.searchBar.text;
// Deletion command
if ([self.searchController.searchBar.text hasSuffix:#"delete"]) {
// Things that I've tried that don't work
//[self.searchController.searchBar setText:#""];
//self.searchController = [self.searchController init];
//self.searchController.searchBar.text = #"";
searchQuery = #"";
}
}
This feature is yet not available in iOS. As soon as you try to clear the text in searchBar's TextField, it change the keyboard mode to keypad input.
So, the conclusion is, you can only provide the input to the textField using the dictation but can't clear it. User has to manually do the process to change again to Dictation Input.
Hope it helps.

AVSpeechSynthesizer going crazy with textView selectedRange

I'm trying to have the AVSpeechSynthesizer read the user's text selection in a UITextView. It works quite well when I double-tap a word but whenever I try to select more than one word, the AVSpeechSynthesizer starts speaking words in a weird way. Is there a way to prevent this behaviour or to retrieve the user's text selection only when they release the selecting finger ?
Any help would be greatly appreciated.
Here is what I do :
-(void) textViewDidChangeSelection:(UITextView *)textView{
NSRange r = textView.selectedRange;
if (!(r.length == 0)) {
NSString *selText = [self.entryTextField2.text substringWithRange:r];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:selText];
[utterance setRate:0.5f];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:self.selectedSpeakerLanguage];
[self.synthesizer speakUtterance:utterance];
}
}
I think I should have done it the Apple way from the start or at least the IOS way. Reading the selectedRange directly from textViewDidChangeSelection: is definitely not a good idea. You need to use a [UIMenuController sharedMenuController] to present a UIMenuItem that triggers the readSelection method instead.

In a Cocoa Mac Os X Objective-C Application, How to implement a Search in the pdf page currently displayed by a PDFView?

I am having problems with searching a PDFView in a Cocoa Application. Now I have set up an IBAction containing the code for selecting a specific word ("continue") that I know resides in the current pdf page in my tests. But The Word is never selected, although my code gathers the PDFSelection objects array correctly, finding only 1 occurrence. My code is :
-(IBAction)sampleAction:(id)sender
{
NSArray *results = [self.pdfView.document findString:#"continue" withOptions:NSCaseInsensitiveSearch];
for (PDFSelection *pdfSel in results)
{
[pdfSel drawForPage:self.pdfView.currentPage active:YES];
}
[self.pdfView scrollSelectionToVisible:self];
}
Nevertheless, in the currently displayed page, the word continue is there, but I get no selection. Please help !
Try finding the next occurrence with:
PDFSelection *pdfSel=[self.pdfView.document findString: #"continue"
fromSelection: self.pdfView.currentSelection
withOptions: NSCaseInsensitiveSearch];
if (pdfSel != nil)
{
[self.pdfView setCurrentSelection: pdfSel];
[self.pdfView scrollSelectionToVisible: self];
}

Change text of a label

I have a array that reads a .txt file and when you click a button the label changes in one of the words of the .txt file, but the label doen't change.
This is the code:
if(sender == self.button) {
NSArray *words = [[NSArray alloc] initWithObjects:#"words.txt", nil];
[randomLabel setText:[words objectAtIndex:random() % [words count]]];
}
What should I do so the label changes when I press the button?
What file do I use?
A few things here:
Reading in file into an array
Well, for starters you're not reading in the contents of the .txt file.
NSArray *words = [[NSArray alloc] initWithObjects:#"words.txt", nil];
This creates a 1 element array, with that one element being #"words.txt". I don't know the format of your .txt file, so I can't say for sure how you have to load it in. See How do I format a text file to be read in using arrayWithContentsOfFile on how to potentially do this.
Setting button text
Also, you need to make sure randomLabel actually refers to the label contained within the button, otherwise the button text won't change. Typically for a button, you'd change the title using the method:
- (void)setTitle:(NSString *)title forState:(UIControlState)state
So in your instance, it'd be:
NSString* newTitle = [words objectAtIndex:random() % [words count]];
[self.button setTitle:newTitle forState:UIControlStateNormal];
Is the code actually being called?
Double check that sender == self.button evaluates to true (for readability and clarity, I'd use [sender isEqual:self.button]). Use the debugger to step through the code, to see if that particular piece of code is being called. See http://mobile.tutsplus.com/tutorials/iphone/xcode-debugging_iphone-sdk/ on how to achieve this.
You should try using
(id)initWithContentsOfFile:(NSString *)aPath

Xcode Like log with NSTextView

I'm trying to set up a NSTextView like the console in Xcode (or pretty much any other IDE available). That being the user cannot edit the NSTextView, however they can put in a character when appropriate, I'm trying to set up that same functionality. No clue how to go about it. Any ideas?
You could simply make an action that appends a formatted string containing a line break, a time stamp, and your desired text to the text view. Here's an example:
- (void)addToLog:(NSString *)input
{
[[self.myTextView textStorage] appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:#"\n%#: %#",[NSDate date],input]]];
}
So then instead of using NSLog(#"some text"); you could call [self addToLog:#"some text"]; and it would be added to a new line in your text view.