How could I outline Text font?

后端 未结 2 1120
你的背包
你的背包 2021-02-06 11:04

I would like to display text with another color in it\'s border (outline). i\'m trying to display a text in MapOverlayView using

[text drawAtPoint:CGPointMake(0         


        
2条回答
  •  情深已故
    2021-02-06 11:25

    The accepted answer didn't work for me possibly because drawAtPoint:withFont: is deprecated. I was able to get it to work with the following code:

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGFloat fontSize = 18.0;
    CGPoint point = CGPointMake(0, 0);
    
    UIFont *font = [UIFont fontWithName:@"Arial-BoldMT" size:fontSize];
    UIColor *outline = [UIColor whiteColor];
    UIColor *fill = [UIColor blackColor];
    
    NSDictionary *labelAttr = @{NSForegroundColorAttributeName:outline, NSFontAttributeName:font};
    
    CGContextSetTextDrawingMode(context, kCGTextStroke);
    CGContextSetLineWidth(context, 2.0);
    [text drawAtPoint:point withAttributes:labelAttr];
    
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextSetLineWidth(context, 2.0);
    labelAttr = @{NSForegroundColorAttributeName:fill, NSFontAttributeName:font};
    [text drawAtPoint:point withAttributes:labelAttr];
    

提交回复
热议问题