ios - Detecting the word taps on attributed text in a cell (Solved) -
this code works else, when i'm trying use on uitextview
placed in cell, cant seem attributed word tapt.
uitapgesturrecognizer
added on cell itself.
this code creates attributed text:
//color: + (nsattributedstring *)attributedmessagefrommessage:(nsstring *)message { nsarray* messagewords = [message componentsseparatedbystring: @" "]; nsmutableattributedstring *attributedmessage = [[nsmutableattributedstring alloc] initwithstring:@""]; (nsstring *word in messagewords) { nsdictionary * attributes; if ([word isequaltostring:@""] || !word) { return attributedmessage; } if([word characteratindex:0] == '@'){ attributes = @{nsforegroundcolorattributename:[uicolor colorwithred:72.0/255.0 green:192.0/255.0 blue:89.0/255.0 alpha:1.0], wordtype: usernamekey, usernamekey:[word substringfromindex:1], nsfontattributename:[uifont fontwithname:@"helveticaneue-light" size:18.0f]}; } else if([word characteratindex:0] == '#'){ attributes = @{nsforegroundcolorattributename:[uicolor colorwithred:16.0/255.0 green:96.0/255.0 blue:184.0/255.0 alpha:1.0], wordtype: hashtagkey, hashtagkey:[word substringfromindex:1], nsfontattributename:[uifont fontwithname:@"helveticaneue-light" size:18.0f]}; } else { attributes = @{ nsforegroundcolorattributename:[uicolor colorwithred:145.0/255.0 green:145.0/255.0 blue:145.0/255.0 alpha:1], wordtype: normalkey, nsfontattributename:[uifont fontwithname:@"helveticaneue-light" size:18.0f]}; } nsattributedstring * substring = [[nsattributedstring alloc] initwithstring:[nsstring stringwithformat:@"%@ ",word] attributes:attributes]; [attributedmessage appendattributedstring:substring]; } return attributedmessage; }
and code called on tap:
//tap gesturerecornizer: + (wordobject *)messagetapped:(uitapgesturerecognizer *)recognizer { uitextview *textview = (uitextview *)recognizer.view; nslayoutmanager *layoutmanager = textview.layoutmanager; cgpoint location = [recognizer locationinview:textview]; location.x -= textview.textcontainerinset.left; location.y -= textview.textcontainerinset.top; nsuinteger characterindex; characterindex = [layoutmanager characterindexforpoint:location intextcontainer:textview.textcontainer fractionofdistancebetweeninsertionpoints:null]; if (characterindex < textview.textstorage.length) { nsrange range; id wordtypetofind = [textview.attributedtext attribute:@"wordtype" atindex:characterindex effectiverange:&range]; if([wordtypetofind isequaltostring:usernamekey]){ nsstring *username = [textview.attributedtext attribute:usernamekey atindex:characterindex effectiverange:&range]; //returning word tappet: wordobject *wordobject = [[wordobject alloc]initwithword:username andtype:name andwhasthereawordclick:yes]; return wordobject; } else if([wordtype isequaltostring:hashtagkey]){ // todo: segue hashtag controller once in place. nsstring *username = [textview.attributedtext attribute:hashtagkey atindex:characterindex effectiverange:&range]; // [self openviewcontrollerforusername:username]; wordobject *wordobject = [[wordobject alloc]initwithword:username andtype:hashtag andwhasthereawordclick:yes]; return wordobject; } } return nil; }
the id "wordtypetofind" returning empty.
i'm thinking maybe because uitextview
placed on cell, need consider ether value?
thanks allot helpers
edit
very strange thing happening i'm checking issue. i'm trying, on tap, insert , attributed text on line after, , i'm getting nil! way happening? , again, nun of happens when attributed string appended uitextview not on cell.
//this called on tap: nsattributedstring *final = [texthandlerhelper attributedtextviewstring:stringseperated]; //final has value of @"bla bla @bla" [self.labelhashtagsbottum setattributedtext:final]; //text value nil! nsstring *text = [self.labelhashtagsbottum.attributedtext string];
found answer problem.
for strange reason, attributed text, whenever used in cell outlet, releases strait away(even if string reference).
i not know way happening, attributed text may still seen, value:
cell.textview.attributedtext
will nill.
so lets say, have uitable instagram has, clickable hashtags on cells. value of attribute text return nill every time.
solution
the way found around add uitextview subview cells contentview.
like so:
[self.textview removefromsuperview]; self.textview = nil; self.textview = [[uitextview alloc]initwithframe:cgrectmake(43, 310, 224, 51)]; [self.contentview addsubview:self.textview]; self.textview.editable = no; [self.textview setbackgroundcolor:[uicolor clearcolor]]; self.textview.scrollenabled = no;
if has better solution, or explanation this, happy hear it..
Comments
Post a Comment