0

I'm using the Acapela TTS on iOS. I have a HTML document with all the text (and markup) that I want read out. The text is split in paragraphs and I want to be able to start the TTS at each header. At the moment I put each paragraph in it's own UIWebView (for handling the internal markup) and add these to a UIScrollView for navigating all the elements. Each UIWebView is overloaded with a button, so when it's pressed, the connected text is read aloud. My problem now, is that I have to place the UIWebView and Buttons using a frame (meaning a static y-position), but this entails that I have the exact height, which is my current problem. I have tried using the solutions mentioned in this question: UIWebView height, but it still doesn't look good - either the webviews are too high or short, and I need it to be perfect, so it looks like a consistent webpage.

Does anybody know of a different way of doing this type of TTS without splitting it up in separate webviews that I have overlooked?

This is my code that place the UIWebViews:

float yOffset = 0;
UIWebView *previous = nil;

//UIWebView delegate
-(void)webViewDidFinishLoad:(UIWebView *)webView{

NSString *height = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];

if(previous!=nil){
    yOffset += previous.frame.size.height;
}
CGFloat h = [height floatValue];

CGRect frame = CGRectMake(0, yOffset, scrollView.frame.size.width, h); 

[webView setFrame:frame];
[webView setOpaque:NO];
[webView setBackgroundColor:[UIColor whiteColor]];
previous = webView;
[scrollView addSubview:webView];

yOffset += previous.frame.size.height;

[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, yOffset)];

}

Community
  • 1
  • 1
MsJorge
  • 155
  • 2
  • 11

1 Answers1

0

Just an idea: you could turn the complete paragraphs into clickable links that point to some identifier of the paragraph. Then you can override the clicking event with UIWebViewDelegate and get the paragraph text with javascript. I mean something like this:

HTML:

<p id="paragraph1"><a href="paragraph1">First paragraph</a></p>
<p id="paragraph2"><a href="paragraph2">Second paragraph</a></p>

Implement shouldStartLoadWithRequest of UIWebViewDelegate:

           - (BOOL)webView:(UIWebView *)webView 
shouldStartLoadWithRequest:(NSURLRequest *)request 
            navigationType:(UIWebViewNavigationType)navigationType 
{
  NSString* link = [request.URL lastPathComponent]; // not sure

  // use rangeOfString to find all
  if (inType == UIWebViewNavigationTypeLinkClicked && [link compare:@"paragraph1"] == NSOrderedSame) 
  { 
    // check this
    NSString* javaScript = 
      [NSString stringWithFormat:@"document.getElementById('%@').nodeValue", link]; 
    NSString paragraphToRead = 
      [webView stringByEvaluatingJavaScriptFromString:javaScript];
    // read out paragraphToRead
    return NO;
  }
}
MrTJ
  • 13,064
  • 4
  • 41
  • 63
  • Its a sound idea, but the problem is, that I can't change the HTML. It is provided by a company, that does not really have the knowledge to create more specific tags.. – MsJorge Apr 27 '12 at 09:06