Does CGContextMoveToPoint work with CGContextShowText? I'm trying to draw to a PDF. Without any translating of the CTM, if I draw text, I see it in the bottom left side of the screen. Then I try to move to point (100,100), and the text is still there. But if I translate the CTM to position 100, 100, then I see the text at that point. Does CGContextMoveToPoint work with CGContextShowText? Otherwise it seems like I translate my CTM, then I need to make the reverse translation, then move it somewhere else to draw other text (like if I were doing a title, and then starting a paragraph). Thanks!
You need to use CGContextSetTextPosition() instead. I don't know why Quartz keeps different positions for text and graphics, but that's the way it is.
Related
I would like to create a widget (I know it must be a CustomPainter) to draw something like this:
Notice that there are n concentric cell circles.
The main problem is that I need to access/paint each of this cells when needed to repaint it in different colors. I have been checking the CustomPainter canvas class and the only thing that could fit my needs is the draw arc thing. But, I can't provide a width for the arc (I tought I could draw a draw for every cell).
My last resort is using an image/sprite that would represent a cell. This way I could duplicate and transform it along a circle path to obtain something like the image. But I don't like the idea.
Has anybody accomplished this before in flutter?
I have a question regarding Cairo codings (http://cairographics.org/).
I have a filled rectangle (cairo_fill (cr)), how can I cut a hole in the middle of it representing for example the letter "S"?
So basically, I want to "engrave" text in that rectangle but also making the text transparent (like cutting a hole in the rectangle).
Anyone have any tips?
Set the fill rule to even-odd and use cairo_text_path() to get the shape of the letter "S", then cairo_fill() as you do now.
I need to render rich text using Core Text in my view (simple formatting, multiple fonts in one line of texts, etc.). I am wondering if text rendered this way can be selected by user using (standard copy / paste function)?
I implemented a text selection in CoreText. It is really a hard work... But it's doable.
Basically you have to save all CTLine rects and origins using CTFrameGetLineOrigins(1), CTLineGetTypographicBounds(2), CTLineGetStringRange(3) and CTLineGetOffsetForStringIndex(4).
The line rect can be calculated using the origin(1), ascent(2), descent(2) and offset(3)(4) as shown bellow.
lineRect = CGRectMake(origin.x + offset,
origin.y - descent,
offset,
ascent + descent);
After doing that, you can test which line has the touched point looping the lines (always remember that CoreText uses inverse Y coordinates).
Knowing the line that has the touched point, you can know the letter that is located at that point (or the nearest letter) using CTLineGetStringIndexForPosition.
Here's one screenshot.
For that loupe, I used the code shown in this post.
Edit:
To draw the blue background selection, you have to paint the rect using CGContextFillRect. Unfortunately, there's no background color in NSAttributedString.
I'm wondering how I should go about drawing a uibezierpath where the stroke width peaks at the center of the arc. Here's an example of what I mean:
Either I have to go through each point when drawing, and set the stroke width accordingly, or there's an easier way. Can anyone point me in the right direction?
Thanks
You can just draw the two outer paths with no stroke, join them, and then fill in the space between them.
Another way to try this if you're interested:
I ended up getting this to work by creating a loop to draw a couple hundred line segments, and changing the line width accordingly during the draw loop.
To adjust the line width I used the following function: MAX_WIDTH * sinf(M_PI * (i/NUMBER_OF_SEGMENTS)
Looks great and no performance issues as far as I can tell. Worked out particularly well because I already had a list of the points to use on the curve. For other cases I'm guessing it would be better to use sosborn's method.
What's the best practice for drawing a closed, filled path where each line has a different stroke?
Fill the complete path, then iterate on its elements to stroke one line segment for every lineto and closepath (see actual Quartz names here). Your applier function will need to keep track of the current point itself.
Of course, if any of the elements are curveto instead of lineto, you may be screwed, but try it anyway.
Incidentally, if your intent is to make marching ants (a selection marquee), there's a much simpler way: Set the line dash, then fill and stroke as normal.