¿Cómo puedo encontrar la posición de píxel de un personaje en un NSTextView?
-
22-08-2019 - |
Pregunta
Actualmente estoy usando el siguiente código en busca de la posición NSView lógica de glifos, que está trabajando muy bien sin embargo, parece un poco lento y las posiciones no están precisamente en el punto carácter ajuste entre caracteres y línea de base. Muy bien puede ser el caso de que todo lo que necesita hacer es iterar los propios glifos uno por uno y construir el mismo RECT.
Este código (funciones) ahora, sólo quiero ver si se puede hacer de manera más eficiente y correctamente.
// lastRange is any valid, bounds checked character range in an NSTextView
NSLayoutManager *layoutManager = [self layoutManager];
NSRect paragraphRect = [layoutManager boundingRectForGlyphRange:lastRange inTextContainer:[self textContainer]];
// Draw a gradient around the range.
NSGradient * gradient = [self indicatorGradient];
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetBlendMode(myContext , kCGBlendModeMultiply);
[gradient drawInRect:paragraphRect angle:90];
Solución
Esta es la manera de hacerlo. Sin embargo, si lastRange es un characterRange como usted describió, usted tiene que poner a través
lastRange = [layoutManager glyphRangeForCharacterRange:lastRange actualRange:NULL];
primero para obtener resultados correctos. también hay que asegurarse de tener en cuenta el origen de la textcontainer el texto está en layouted y luego hacer un
NSPoint containerOrigin = [[container textView] textContainerOrigin];
paragraphRect = NSOffsetRect(paragraphRect,containerOrigin.x,containerOrigin.y);
antes de dibujar.