The detailed explanation and example _ios of IOS graphics and text mixed row (coretext.framework)

Source: Internet
Author: User

IOS Graphics and Text Mix (coretext.framework)

This article mainly introduces the iOS graphics and text mixed row of data, here sorted out the content of the online search, to help understand, master this part of knowledge, the following is the contents of the collation:

The use of Coretext for graphic mixed row.

Implementation code:

void Rundelegatedealloccallback (void* refcon) { 
   
} 
 
cgfloat rundelegategetascentcallback (void *refcon) { 
  NSString *imagename = (NSString *) Refcon; 
  return 80;//[uiimage imagenamed:imagename].size.height; 
} 
 
CGFloat rundelegategetdescentcallback (void *refcon) {return 
  0; 
} 
 
CGFloat rundelegategetwidthcallback (void *refcon) { 
  NSString *imagename = (NSString *) Refcon; 
  return 100;//[uiimage Imagenamed:imagename].size.width; 

Set up a Ctrun delegate, which is used primarily to specify the uplink height, width, or context release of the object.

-(void) Drawcharandpicture {cgcontextref context = Uigraphicsgetcurrentcontext (); Cgcontextsettextmatrix (context, cgaffinetransformidentity);//Set Glyph transformation matrix to Cgaffinetransformidentity, 
  That is to say, each glyph does not do a graphic transformation cgaffinetransform flipvertical = Cgaffinetransformmake (1,0,0,-1,0,self.bounds.size.height); 
   
  CGCONTEXTCONCATCTM (context, flipvertical); Flip NSLog The coordinate system of the current context (@ "bh=%f", self.bounds.size.height); nsmutableattributedstring *attributedstring = [[[Nsmutableattributedstring alloc] initwithstring:@ "Please insert a picture position here"] 
   
   
  Autorelease]; 
  Set ctrundelegate,delegate for the picture nsstring *imgname = @ "Img.png" for the space size of the picture; 
  Ctrundelegatecallbacks imagecallbacks; 
  Imagecallbacks.version = KCTRunDelegateVersion1; 
  Imagecallbacks.dealloc = Rundelegatedealloccallback; 
  Imagecallbacks.getascent = Rundelegategetascentcallback; 
  Imagecallbacks.getdescent = Rundelegategetdescentcallback; 
  Imagecallbacks.getwidth = Rundelegategetwidthcallback; Ctrundelegateref RundelegatE = Ctrundelegatecreate (&imagecallbacks, imgname); nsmutableattributedstring *imageattributedstring = [[Nsmutableattributedstring alloc] initwithstring:@ ""];// Spaces are used to leave a picture in place [imageattributedstring addattribute: (NSString *) Kctrundelegateattributename value: (ID) rundelegate range: 
  Nsmakerange (0, 1)]; 
   
  Cfrelease (rundelegate); 
   
  [Imageattributedstring addattribute:@ "imagename" Value:imgname range:nsmakerange (0, 1)];  [Attributedstring insertattributedstring:imageattributedstring Atindex:4];
 Ctparagraphstylesetting Linebreakmode of line-wrapping mode; 
  Ctlinebreakmode linebreak = kctlinebreakbycharwrapping; 
  Linebreakmode.spec = Kctparagraphstylespecifierlinebreakmode; 
  Linebreakmode.value = &lineBreak; 
   
  linebreakmode.valuesize = sizeof (Ctlinebreakmode); 
   
  Ctparagraphstylesetting settings[] = {Linebreakmode}; 
   
     
  Ctparagraphstyleref style = Ctparagraphstylecreate (settings, 1); Build attributes Nsmutabledictionary *attributes = [Nsmutabledictionary dictionarywithobject: (ID) style Forkey: (ID) k 
   
  Ctparagraphstyleattributename]; Set attributes to attributed string [attributedstring addattributes:attributes range:nsmakerange (0, [Attributedstrin 
   
 
   
  G length])]; Ctframesetterref Ctframesetter = ctframesettercreatewithattributedstring ((cfmutableattributedstringref) 
   
  attributedstring); 
  Cgmutablepathref path = cgpathcreatemutable (); CGRect bounds = cgrectmake (0.0, 0.0, self.bounds.size.width, self.bounds.size.height); 
  Cgpathaddrect (path, NULL, bounds); 
  Ctframeref ctframe = ctframesettercreateframe (ctframesetter,cfrangemake (0, 0), path, NULL); 
   
  Ctframedraw (ctframe, context); 
  Cfarrayref lines = Ctframegetlines (ctframe); 
  Cgpoint Lineorigins[cfarraygetcount (lines)]; 
  Ctframegetlineorigins (Ctframe, cfrangemake (0, 0), lineorigins); 
  NSLog (@ "line count =%ld", Cfarraygetcount (lines)); 
    for (int i = 0; i < cfarraygetcount (lines); i++) {Ctlineref line = Cfarraygetvalueatindex (lines, i); 
    CGFloat lineascent; 
    CGFloat linedescent; 
    CGFloat lineleading; 
    Ctlinegettypographicbounds (line, &lineascent, &linedescent, &lineleading); 
     
    NSLog (@ "ascent =%f,descent =%f,leading =%f", lineascent,linedescent,lineleading); 
    Cfarrayref runs = ctlinegetglyphruns (line); 
    NSLog (@ "Run count =%ld", Cfarraygetcount (runs)); 
      for (int j = 0; J < Cfarraygetcount (runs); j + +) {cgfloat runascent; 
      CGFloat rundescent; Cgpoint LIneorigin = Lineorigins[i]; 
      Ctrunref run = Cfarraygetvalueatindex (runs, j); 
      nsdictionary* attributes = (nsdictionary*) ctrungetattributes (run); 
      CGRect Runrect; 
      RunRect.size.width = Ctrungettypographicbounds (Run, Cfrangemake (0,0), &runascent, &rundescent, NULL); 
       
      NSLog (@ "width =%f", runRect.size.width); Runrect=cgrectmake (lineorigin.x + ctlinegetoffsetforstringindex (line, Ctrungetstringrange (run). Location, NULL), 
       
      Lineorigin.y-rundescent, RunRect.size.width, runascent + rundescent); 
      NSString *imagename = [Attributes objectforkey:@ "imagename"]; 
        Picture rendering logic if (imagename) {uiimage *image = [UIImage imagenamed:imagename]; 
          if (image) {CGRect imagedrawrect; 
          Imagedrawrect.size = image.size; 
          imagedrawrect.origin.x = runrect.origin.x + lineorigin.x; 
          IMAGEDRAWRECT.ORIGIN.Y = LINEORIGIN.Y; Cgcontextdrawimage (context, imagedrawrect, image. CgiMage); 
  }}} cfrelease (Ctframe); 
  Cfrelease (path); 
Cfrelease (Ctframesetter);  }

Effect:


From the above, you may not find any problems, when the picture on the left side of the word will look like?


So to avoid this, I added a newline pattern to the code. Add the effect after wrapping:


Thank you for reading, I hope to help you, thank you for your support for this site!

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.