2012-07-31 12 views
6

Ho due stringhe attribuite che dicono "A" e "."Testo principale - Altezza del glifo

Ho bisogno di calcolare l'altezza di ciascuna di queste stringhe. Attualmente l'altezza restituita è la stessa per entrambi, sembra restituire l'altezza massima possibile per il carattere più alto in un determinato font (anche se quel carattere non è presente nella stringa).

Mi piacerebbe ottenere l'esatta altezza del pixel per ciascuno di questi caratteri, in modo da poter ridimensionare una vista intorno a loro che si adatta perfettamente al carattere (glifo). Ho provato a utilizzare CTFramesetterSuggestFrameSizeWithConstraints() e CTLineGetTypographicBounds() ma restituisce un numero simile al metodo di dimensioni stringhe attribuito.

Apprezzerebbero qualsiasi suggerimento su come fare per fare questo!

risposta

7

arrivati ​​alla fine, si può fare in questo modo:

// Create an attributed string 
CTLineRef line = CTLineCreateWithAttributedString(_string); 

// Get an array of glyph runs from the line 
CFArrayRef runArray = CTLineGetGlyphRuns(line); 

// loop through each run in the array  
CTRunRef run = .... 

// Get the range of the run   
CFRange range = CFRangeMake... 

// Use CTRunGetImageBounds         
CGRect glyphRect = CTRunGetImageBounds(run, context, range); 

// glyphRect now contains the bounds of the glyph run, if the string is just 1 character you have the correct dimensions of that character. 
Problemi correlati