djemotionpageview.m
// Send click-to-broadcast (similar to Android, the difference is that Android broadcasts are as long as there are contextual objects in context and can be sent) // notifications sent and received in iOS are done via Nsnotificationcenter Nsmutabledictionary *userinfo = [nsmutabledictionary dictionary]; = btn.emotion; Object: nil Userinfo:userinfo];
djcomposeviewcontroll.m
-(void // Register for listener emoji button click Notifications [[ Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (onreceiveemotionbtnclick:) name:d Jemotiondidselectednotification object :nil]; /* * Receive emoticons button click Notifications */-(void ) Onreceiveemotionbtnclick: (Nsnotification *< Span style= "COLOR: #000000" >) notification {nsdictionary *intent = notification.u Serinfo; Djemotion *emotion = Intent[djemotiondidselctedemotionkey]; [Self.textview insertemotion:emotion]; }
djemotiontextview.m
- (void) Insertemotion: (Djemotion *) Emotion {//Insert Emoticons if(Emotion.code) {//Emoji Expression[self inserttext:[nsstring emojiWithStringCode:emotion.code]]; } Else if(emotion.png) {//Emoticons PicturesNSString *emotionname =emotion.png; NSString*ImagePath; if([Emotionname Hasprefix:@"D_"] || [Emotionname Hasprefix:@"f_"] ||[emotionname hasprefix:@"H_"] || [Emotionname Hasprefix:@"L_"] || [Emotionname Hasprefix:@"O_"] || [Emotionname Hasprefix:@"W_"]) {ImagePath= [NSString stringWithFormat:@"emotionicons/default/%@", Emotion.png];//default emoticon Path}Else if([Emotionname Hasprefix:@"Lxh_"]) {ImagePath= [NSString stringWithFormat:@"emotionicons/lxh/%@", Emotion.png];//wave floret expression path } //construct a attrstr based on the original content of TextViewNsmutableattributedstring *attrstr =[[Nsmutableattributedstring alloc] initWithAttributedString:self.attributedText]; //Structural Emoticons AttachmentNstextattachment *attachment =[[Nstextattachment alloc] init]; Attachment.image=[UIImage Imagenamed:imagepath]; CGFloat Attachmentwh=Self.font.lineHeight; Attachment.bounds= CGRectMake (0, -4, Attachmentwh, Attachmentwh); //Connect emoji PicturesNsattributedstring *attachstr =[nsattributedstring attributedstringwithattachment:attachment]; //Insert attachment text where the cursor is locatedNsuinteger CursorLocation =self.selectedRange.location; [Attrstr insertattributedstring:attachstr atindex:cursorlocation]; //sets the font for the current attrstr, because ATTRSTR fonts cannot be set by the Textview.font property[Attrstr addattribute:nsfontattributename Value:self.font Range:nsmakerange (0, Attrstr.length)]; //Update current TextView contentSelf.attributedtext =Attrstr; //corrects the current cursor position (moves the cursor to the end of the insertion emoticon, and the default cursor jumps to the end of all text)Self.selectedrange = Nsmakerange (CursorLocation +1,0); } }
Final effect:
Sina Weibo Client (47)-insert expression in TextView