2010-09-22 5 views
9

Estoy tratando de averiguar cómo tomar un NSAttributedString y usarlo en Core Text en el iPad. Vi uno de los videos WWDC (110), que tiene toboganes (pero no el código fuente) y que describe cómo crear un NSAttributedString, a continuación, sólo hay que poner en un CTFramesetterRef:¿Puedo usar NSAttributedString en Core Text en iOS?

CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 24.0, NULL); 

NSString* unicodeString = NSLocalizedString(@"TitleString", @"Window Title"); 
CGColorRef color = [UIColor blueColor].CGColor; NSNumber* underline = [NSNumber numberWithInt:kCTUnderlineStyleSingle|kCTUnderlinePatternDot]; 
NSDictionary* attributesDict = [NSDictionary dictionaryWithObjectsAndKeys:helveticaBold, (NSString*)kCTFontAttributeName, color, (NSString*)kCTForegroundColorAttributeName, underline, (NSString*)kCTUnderlineStyleAttributeName, nil]; 
NSAttributedString* stringToDraw = [[NSAttributedString alloc] initWithString:unicodeString attributes:attributesDict]; 

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(stringToDraw); 

CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL); 
CFRelease(framesetter); 
CTFrameDraw(frame, context); 
CFRelease(frame); 

Pero cuando intento esto, lanza el error:

Passing argument 1 of 'CTFramesetterCreateWithAttributedString' from incompatible pointer type

Se CFAttributedString y NSAttributedString 'gratuito en puente'? He leído en alguna parte que esto solo es cierto en OSX, pero así es como lo hace Apple en su demo ...

¡Gracias!

: -Joe

+0

que eran capaces de obtener el subrayado de puntos a aparecer? Solo obtengo el subrayado simple bajo el texto. – learner2010

Respuesta

7

Puede descargar el código fuente WWDC10 here. Además, use el puente gratuito y explícitamente envíe su stringToDraw a CFAttributedStringRef.

+0

Gracias, pero el enlace que me dio sigue diciendo que mi sesión ha expirado (incluso cuando intento iniciar sesión de nuevo). He descargado el código de ejemplo WWDC10 antes, pero 110 no estaba en él :( – jowie

+1

Aparte de eso, su solución funcionó, ¡gracias! :-) – jowie

+1

podría publicar código de trabajo final. – user836026

3

Estás casi allí. Simplemente eche el CTFontRef a un tipo id cuando lo agrega al diccionario de atributos.

CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 24.0, NULL); 

NSString* unicodeString = NSLocalizedString(@"TitleString", @"Window Title"); 

CGColorRef color = [UIColor blueColor].CGColor; 
NSNumber* underline = [NSNumber numberWithInt: 
    kCTUnderlineStyleSingle|kCTUnderlinePatternDot]; 

NSDictionary* attributesDict = [NSDictionary dictionaryWithObjectsAndKeys: 
        (id)helveticaBold, (NSString*)kCTFontAttributeName, 
        color, (NSString*)kCTForegroundColorAttributeName, 
        underline, (NSString*)kCTUnderlineStyleAttributeName, nil]; 

NSAttributedString* stringToDraw = [[NSAttributedString alloc] 
          initWithString:unicodeString 
          attributes:attributesDict]; 
Cuestiones relacionadas