简体   繁体   中英

Determine the CTLine containg specific character

I am developing an Application in which I am using Core Text for layout purpose. I have used CTFrameSetter to draw the text.I have inserted a blank character '\' in my text. Now what i want is to determine the position of this blank character ie its x, y coordinates. I have not been able to find any function to determine the position of any specific character.

I tried using CTLineGetOffsetForStringIndex( CTLineRef line, CFIndex charIndex, CGFloat* secondaryOffset ); for finding the position, but I couldn't fully understand the working of this method.

Can anyone provide me some pointers on this issue?

you can loop all the CTLines of the current CTFrameRef, and for each ctline, get the CTRun and check if that CTRun is yours. simple code

 NSArray *lines = (NSArray*)CTFrameGetLines(workFrame);
 NSInteger count = [lines count];
 CGPoint *origins = (CGPoint*)malloc(count * sizeof(CGPoint));
 CTFrameGetLineOrigins(workFrame, CFRangeMake(0, count), origins); 

 for (int i = 0; i < lines.count; i++) {
      CTLineRef line = (CTLineRef)[lines objectAtIndex:i];
      CFRange cfRange = CTLineGetStringRange(line);//the string-range

       NSArray * runArray = (NSArray *)CTLineGetGlyphRuns(line);

        int runIndex = 0;
        for (id runObj in runArray) {
          CTRunRef run = (CTRunRef)runObj;          
          CFRange runRange = CTRunGetStringRange(run);
          CGFloat xOffset = CTLineGetOffsetForStringIndex(line, runRange.location + runRange.length, NULL);
          //checks if the crrun's text is your space
        }
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM