ios - Highlight attributedtext UITextView using NSRanges to an array value? -
haven't had luck in searching, hoping make story text animate (change colour)in sync audio clip quite stuck how (i'm quite new @ coding)
i'm stuck how find correct range, word gets coloured , how update string in sync .caf; how connect , colour correct ranges im guessing run action duration (using audiointervals array timing)
// int wordcount = 0; int wordcount = 0; int indexcount = 0; //for (nsstring *word in wordsinarray) (nsmutableattributedstring *word in wordsinarray) //*wordsinarray = componentsseparatedby whitespace { if (wordcount < [audiointervals count]) //wordcount less audiointervals { nsstring *orange = @"orange\n\n";//change text orange // action duration here on //original mutable string block/duration/delay audiointerval //how range***********the bit i'm stuck on********** [attstring addattributes:colour01 range:nsmakerange(0,???)]; [attstring addattributes:colour02 range:nsmakerange(???,???)]; [attstring addattributes:colour01 range:nsmakerange(???,???)]; //************************************************************* mytext.attributedtext = attstring;
i'm confused how range works - how can use interval times range? correct word picked coloured or returned original colour) }
...more code wordcount++; nslog(@"time %@, %i, index %@\n\n",word,wordcount,[audiointervals objectatindex:indexcount]); indexcount++; rangestart = rangeend +1;
} }
log shows audiointerval duration, time text colour, colour + word count how right range on uitextfield?? on stuck!
//which shows plists , word count //and colourswap logic working duration 0.176387 @colour orange time 0.176387 @colour black this, 1, duration 0.274997 @colour orange time 0.274997 @colour black is, 2, duration 0.345829 @colour orange time 0.345829 @colour black the, 3, duration 0.612493 @colour orange time 0.612493 @colour black text, 4, duration 0.9763770000000001 @colour or time 0.9763770000000001 @colour black for, 5,
//in simple tests can colour few words in sentence how can use update coloured word sound timing see "boldtextinrangewithcolour" test,
//my small test of applying style text in uitextview //its result photo see
-(void)boldtextinrangewithcolour { nsstring *infostring =content;
//nsmutableattributedstring alows bold styling nsmutableattributedstring *attstring= [[nsmutableattributedstring alloc] initwithstring:infostring]; //styling uifont *font_regular= [uifont fontwithname:@"avenir-light" size:30.0f]; uifont *font_bold= [uifont fontwithname:@"avenirnext-bold" size:30.0f]; uicolor* textcolorblack = [uicolor blackcolor]; uicolor* textcolorred = [uicolor redcolor]; nsdictionary *colour01 = @{ nsforegroundcolorattributename : textcolorblack, nsfontattributename : font_regular, }; nsdictionary *colour02 = @{ nsforegroundcolorattributename : textcolorred, nsfontattributename : font_regular, }; /*attstring nsmutableattributedstring paragraph of story text*/ [attstring addattributes:colour01 range:nsmakerange(0, 4)]; //black //how ranges work can use audiointervals or wordcount //to find correct word colour [attstring addattributes:colour02 range:nsmakerange(5, 15)]; //orange [attstring addattributes:colour01 range:nsmakerange(16, infostring.length - 15 - 1)];//black mytext.attributedtext = attstring; }
if use avaudioplayer
play sounds, can use avaudioplayerdelegate
method, audioplayerdidfinishplaying:successfully:
know when sound has stopped playing, , move next word.
this more robust relying on timing, , if audio or changes, don't need update timing data.
edit
before play story, use enumeratesubstringsinrange:options:usingblock: find range of each word , store in array. use option nsstringenumerationbywords
enumerate each word. block give range each word finds , can store in array.
when story plays, after each nstimer
expiration (created array of word timings), highlight next word.
you use dispatch_after()
instead of nstimer
.
Comments
Post a Comment