2012-04-05 36 views
6

Tôi muốn hiển thị văn bản bằng một màu khác trong đường viền của nó (đường viền). tôi đang cố gắng để hiển thị một văn bản trong MapOverlayView sử dụngLàm cách nào tôi có thể phác thảo phông chữ?

[text drawAtPoint:CGPointMake(0,30) withFont:[UIFont fontWithName:@"Helvetica-Bold" size:(3 * MKRoadWidthAtZoomScale(zoomScale))] 

nó hoạt động tốt, ngoại trừ tôi cần văn bản được đề cương trưng bày.

Trả lời

8

Có, bạn có thể hiển thị văn bản được phác thảo với sự trợ giúp của CGContextSetDrawingMode(CGContextRef, CGTextDrawingMode), mặc dù có thể bạn sẽ cần phải điều chỉnh một số số và màu sắc để làm cho nó trông đẹp.

Điều này có vẻ hợp lý khi sử dụng kCGTextFillStroke, nhưng điều đó có thể khiến cho hành trình vượt quá mức lấp đầy. Nếu bạn đột quỵ, sau đó điền vào, như trong khối dưới đây, bạn sẽ có được một phác thảo có thể nhìn thấy đằng sau văn bản có thể đọc được.

CGContextRef context = UIGraphicsGetCurrentContext(); 

CGPoint point = CGPointMake(0,30); 
CGFloat fontSize = (3 * MKRoadWidthAtZoomScale(zoomScale)); 
UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:fontSize]; 

// Draw outlined text. 
CGContextSetTextDrawingMode(context, kCGTextStroke); 
// Make the thickness of the outline a function of the font size in use. 
CGContextSetLineWidth(context, fontSize/18); 
CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]); 
[text drawAtPoint:point withFont:font]; 

// Draw filled text. This will make sure it's clearly readable, while leaving some outline behind it. 
CGContextSetTextDrawingMode(context, kCGTextFill); 
CGContextSetFillColorWithColor(context, [[UIColor blueColor] CGColor]); 
[text drawAtPoint:point withFont:font]; 
+0

Rất cám ơn ... nó đã hoạt động rất tốt !! – user836026

+2

không hoạt động trong trường hợp của tôi, tôi đã làm theo các bước tương tự –

0

Câu trả lời được chấp nhận không hoạt động vì tôi có thể vì không được chấp nhận vì drawAtPoint:withFont:. Tôi có thể làm cho nó hoạt động với mã sau:

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]; 
Các vấn đề liên quan