Como faço para encontrar a posição de pixel de um personagem em um NSTextView?
-
22-08-2019 - |
Pergunta
Atualmente estou usando o seguinte código para buscar a posição NSView lógica de glifos que está funcionando bem no entanto, parece um pouco lento e as posições não são precisamente sobre o personagem ponto e linha de base kerning. Ele pode muito bem ser o caso que todos necessidade I a fazer é iterar os próprios glifos um por um e construir o rect mim mesmo.
Este código (funções) agora, só quero ver se ele pode ser feito de forma mais eficiente ou corretamente.
// 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];
Solução
Esta é a maneira de fazê-lo. No entanto, se LaStrange é um characterRange como você descreveu, você tem que colocá-lo através
lastRange = [layoutManager glyphRangeForCharacterRange:lastRange actualRange:NULL];
primeiro a obter resultados corretos. você também precisa ter certeza de que conta para a origem do textcontainer o texto é layouted em e depois fazer um
NSPoint containerOrigin = [[container textView] textContainerOrigin];
paragraphRect = NSOffsetRect(paragraphRect,containerOrigin.x,containerOrigin.y);
Antes de desenho.