Sina Weibo Client (47)-insert expression in TextView

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.