objective c - Multiple colors in NSTextField -
i need change color words repeating in each line in nstextfield. i'm using dictionary separate each line , dictionary has , array separates words data, i'm checking if words repeated , want change color or highlight words repeating.. not sure how it.
the program word frequency , show type-token, mean frequency , words repeating (text colour/highlighted)
any great. right i'm assigning text text field .txt file imported. got repeating words , everything.. need show which.
current code:
-(nsmutabledictionary*) computewords:(nsmutabledictionary *)linesdic{ int count = (int)[linesdic count]; int numberofwords = 0; int numberofdiffwords = 0; nsmutabledictionary* wordcomputation = [[nsmutabledictionary alloc]init]; nsmutabledictionary* duplicates = [[nsmutabledictionary alloc]init]; (int x = 1; x<=count; x++) { nsmutablearray* duplicatewords = [[nsmutablearray alloc]init]; nsmutablearray* counter = [[nsmutablearray alloc]init]; nsstring* key = [nsstring stringwithformat:@"%d", x]; nsarray* data = [linesdic objectforkey:key]; numberofwords = (int)[data count]; numberofdiffwords = numberofwords; (int j = 0; j<numberofwords; j++) { (int k = j+1; k<numberofwords; k++) { if([[data objectatindex:k]isequaltostring:[data objectatindex:j]]) { numberofdiffwords--; [duplicatewords addobject:[data objectatindex:k]]; // nslog(@"%@", [data objectatindex:k]); // nslog(@"%@", [data objectatindex:j]); }else{ nsstring* = [data objectatindex:k]; nsstring* b = [data objectatindex:j]; if(a.length<b.length){ if((b.length - a.length)<=4){ if([[b substringtoindex:a.length]isequaltostring:a]) { // nslog(@"key: %@ comapare b:%d:%@ , a:%d:%@", key,(int)b.length, b, (int)a.length, ); // nslog(@"key: %@, %@", key, [b substringfromindex:(a.length-1)]); if([[b substringfromindex:(a.length)]isequaltostring:@"s"] || [[b substringfromindex:(a.length)]isequaltostring:@"es"]){ if (([a isequaltostring:@"a"] && [b isequaltostring:@"as"])) { // nslog(@"break @ key: %@, words %@:%@",key, a, b); break; } numberofdiffwords--; [duplicatewords addobject:[data objectatindex:k]]; [duplicatewords addobject:[data objectatindex:j]]; // nslog(@"%@", [data objectatindex:k]); // nslog(@"%@", [data objectatindex:j]); } } } }else{ if((a.length - b.length)<=4){ if([[a substringtoindex:b.length]isequaltostring:b]) { // nslog(@"key: %@ compare a:%d: %@ , b:%d: %@", key,(int)a.length, a, (int)b.length, b ); // nslog(@"key: %@, %@", key, [a substringfromindex:(b.length-1)]); if([[a substringfromindex:(b.length)]isequaltostring:@"s"] || [[a substringfromindex:(b.length)]isequaltostring:@"es"]){ if (([a isequaltostring:@"as"] && [b isequaltostring:@"a"])) { // nslog(@"break @ key: %@, words %@:%@",key, a, b); break; } numberofdiffwords--; [duplicatewords addobject:[data objectatindex:k]]; [duplicatewords addobject:[data objectatindex:j]]; // nslog(@"%@", [data objectatindex:k]); // nslog(@"%@", [data objectatindex:j]); } } } } } } } nsnumber* nw = [nsnumber numberwithint:numberofwords]; nsnumber* ndw = [nsnumber numberwithint:numberofdiffwords]; [counter addobject:nw]; [counter addobject:ndw]; nslog(@"key: %@, data: %@", key, duplicatewords); [duplicates setobject:duplicatewords forkey:key]; [wordcomputation setobject:counter forkey:key]; } _duplicates = duplicates; nslog(@"%@", _duplicates); _wordcomputation = wordcomputation; return wordcomputation; } -(void)showtable:(nsmutabledictionary*)wordcomputation{ int count = (int)[wordcomputation count]; int numberofwords = 0; int numberofdiffwords = 0; // nslog(@"\t \t diff words words type token \t mean"); (int x = 1; x<=count; x++) { nsstring* key = [nsstring stringwithformat:@"%d", x]; nsarray* data = [wordcomputation objectforkey:key]; numberofwords = [[data objectatindex:0]intvalue]; numberofdiffwords = [[data objectatindex:1]intvalue]; //print log // nslog(@"line: %@ \t %@ \t %@ \t %@ \t\t %@", key, numberofdiffwords<10?[nsstring stringwithformat:@"0%d", numberofdiffwords]:[nsstring stringwithformat:@"%d", numberofdiffwords], numberofwords<10?[nsstring stringwithformat:@"0%d", numberofwords]:[nsstring stringwithformat:@"%d", numberofwords],[nsstring stringwithformat:@"%d/%d", numberofdiffwords, numberofwords], [nsstring stringwithformat:@"%d/%d", numberofwords, numberofdiffwords]); } [_frequencytable reloaddata]; }
tried change color string first shows
{ nscolor = "nscalibratedrgbcolorspace 1 0 0 1"; }
on 5th position in string
- (ibaction)viewduplicatespressed:(id)sender { [_duplicatestextfield sethidden:no]; nslog(@"%@", _duplicates); nsmutableattributedstring *attributedstring = [[nsmutableattributedstring alloc]initwithstring:[nsstring stringwithformat:@"%@", [_contenttextfield stringvalue]] /*attributes:@{nsforegroundcolorattributename : [nscolor redcolor]}*/]; [attributedstring addattribute:nsforegroundcolorattributename value:[nscolor redcolor] range:nsmakerange(0,5)]; [_duplicatestextfield setstringvalue:[nsstring stringwithformat:@"%@", attributedstring]]; }
also tried
int linecount = (int)[_linesdic count]; nsstring* key; nsmutabledictionary* duplidic = [[nsmutabledictionary alloc]init]; (int = 0; i<linecount; i++) { nsarray *duplicatewords; key = [nsstring stringwithformat:@"%d", i]; duplicatewords = [_duplicates objectforkey:key]; nsmutableattributedstring *attributedstring; nsstring* stringforkey = [nsstring stringwithformat:@"%@", [_linesdic objectforkey:key]]; (int j =0 ; < (int)[duplicatewords count]; j++) { nsstring* word = [duplicatewords objectatindex:j]; nsrange range = [stringforkey rangeofstring:word]; attributedstring = [[nsmutableattributedstring alloc]initwithstring:[nsstring stringwithformat:@"%@", [_linesdic objectforkey:key]] /*attributes:@{nsforegroundcolorattributename : [nscolor redcolor]}*/]; [attributedstring addattribute:nsforegroundcolorattributename value:[nscolor redcolor] range:range]; } [duplidic setobject:attributedstring forkey:key]; } [_duplicatestextfield sethidden:no]; nslog(@"%@", duplidic); [_duplicatestextfield setstringvalue:[nsstring stringwithformat:@"%@", duplidic]];
Comments
Post a Comment