I'm using MKMap with overlay. The overlay display text. I need the text to have "stroke" effect. any clue?
[t drawAtPoint:CGPointMake(0,30) withFont:[UIFont fontWithName:#"Helvetica-Bold" /*"Arial"*/ size:(3 * MKRoadWidthAtZoomScale(zoomScale))]
];
If you're drawing in drawRect:, you can set the text drawing mode using:
CGContextSetTextDrawingMode(UIGraphicsGetCurrentContext(), kCGTextStroke);
If you wanted to fill and stroke, then you can use kCGTextFillStroke.
You can create an CFAttributedString from your string, set appropriate kCTStrokeWidthAttributeName and kCTStrokeColorAttributeName attribute values and draw it with CoreText framework.
Related
I created a new class named MySegment which inherits from UIControl.
First, I initialized a gradient line with CAGradientLayer.
Second,[self.layer addSublayer:myLayer]
Then, ["myString" drawInRect:rect withAttributes:attributes] is in the same location.
I want the text from drawInRect to be overlaid on myLayer, but no matter what I use addSublayer or insertlayer,my text is always under mylayer, the effect is shown on the photo.
How can I make my text above the gradient layer?
I'd indeed use CATextLayer as #DonMag has pointed out. In that case, you would have CATextLayer and CAGradientLayer.
First insert the CAGradientLayer using addSubLayer
[self.layer addSubLayer:gradientLayer];
Then the CATextLayer using insertSubLayer:above:
[self.layer insertSubLayer:textLayer above:gradientLayer];
PS: Answered question about putting attributedstring in CATextLayer:
Displaying an attributed string in a CATextLayer
I want to manipulate individual character of UITextView like rotating one to the left(90 degrees) and one to the right(90 degrees), and so on.
Anyone has an idea on achieving this?
The easiest thing to modify individual characters in an UITextView is to use NSMutableAttributedString by extending it like this:
#implementation NSMutableAttributedString (Rotation)
- (void)setRotatedFontWithName:(NSString*)fontName
size:(CGFloat)size
range:(NSRange)range
{
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2);
CTFontRef aFont = CTFontCreateWithName((CFStringRef)fontName, size, &transform);
if (!aFont)
return;
[self removeAttribute:(NSString*)kCTFontAttributeName range:range];
[self addAttribute:(NSString*)kCTFontAttributeName value:(id)aFont range:range];
CFRelease(aFont);
}
And then using the attributedText property of a UITextField to set the rotated font.
What you could also try is creating a custom class that overrides UITextField's default drawRect: and draw the normal and rotated text using Core Text methods (see the example in here, section about rendering text on a curved shape)
I know what I want to do, but I'm not sure how to achieve it. I'm new to Core Graphics and I'm not sure this is even possible.
I have a unicode symbol like this: ♖
The white parts of this symbol are transparent and I'm drawing it in a transparent rect like so:
- (void)drawRect:(CGRect)rect
{
NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
textStyle.alignment = NSTextAlignmentCenter;
UIFont* myFont = [UIFont fontWithName:#"ArialUnicodeMS" size:80];
NSDictionary *attrs =
#{ NSForegroundColorAttributeName : [UIColor blackColor],
NSFontAttributeName:myFont,
NSParagraphStyleAttributeName:textStyle
};
[[self getStringForValue:_squareValue] drawInRect:rect withAttributes:attrs];
}
What I want is to fill the internal transparent sections with white and leave the surrounding rectangle as transparent.
The approach I'm thinking of is using the unicode symbol as a mask in drawRect to divide the rectangle into regions. Then not filling the region starting at 0,0 but filling all other regions. Can I use Core Graphics to convert the symbol to a mask and then identify and fill specific regions?
What I'm getting at the moment is this:
But what I actually want is this:
You might be able to use -[NSBezierPath appendBezierPathWithGlyph:inFont] and then just set the color to white and call -fill on that path.
EDIT: Since you're on iOS, and I don't know how to get the path of a glyph on iOS, instead I'd just draw the glyph to a buffer that's filled with 100% opaque white, and then do a flood-fill starting from 0,0 with pure transparency.
ANOTHER EDIT: With jrturton's code below, you should be able to trivially do a white fill on the path as originally suggested, then stroke it in black.
Within my app, I use drawRect to draw some text within a UIImage. It writes multiple things in multiple places.. Later, I try to erase some of the text by using
CGContextSetBlendMode(context, kCGBlendModeClear);
CGPoint daPoint = CGPointMake(second.x + 20, second.y + 20);
NSDictionary *textAttributes = #{ NSFontAttributeName: [UIFont boldSystemFontOfSize:25.0],
NSForegroundColorAttributeName: [UIColor clearColor] };
[textString drawAtPoint:daPoint withAttributes:textAttributes];
This works almost perfectly, except there is a small thin stroke left over from the text. I use the same code to draw the text, as I do to erase, except when i'm drawing I use kCGBlendModeNormal. How would I get rid of it completely? Can I draw a box and fill it using kCGBlendModeClear? This is what it looks like currently before erasing:
After erasing:
I would get the bounding rect of the text and then call CGContextClearRect and then fill with the background color if you think that "erasing" the text is really necessary. If you simply "redraw" your rect, that might be another way to solve this problem.
Here's how to get that bounding box for clearing:
CGSize textSize = [textString sizeWithAttributes:textAttributes];
CGRect textFrame = CGRectMake(daPoint.x, daPoint.y, textSize.width, textSize.height);
Hope this helps!
I'm trying to draw a UIlabel into a new Context, at a specific location. I know you can draw just the NSString into a context easily, but I want to retain the text size, color, and word wrap style of the UILabel. Thanks for your help!
UIGraphicsBeginImageContext(targetSize);
// here is where I want to draw UILabel to context
UIGraphicsEndImageContext();
You could draw any view using -renderInContext:, but if all you need is just retain the text size, color and word wrap style, you could simply use -drawInRect:withFont:lineBreakMode:alignment: to customize all options.
[label.textColor set];
[label.text drawInRect:label.bounds
withFont:label.font
lineBreakMode:label.lineBreakMode
alignment:label.textAlignment];
There is also a -drawTextInRect: method in UILabel, but Apple says "You should not call this method directly."