2011-01-12 31 views
29

Tôi đã tự hỏi làm thế nào tôi có thể tạo đột quỵ văn bản cho UILabel trong iOS4? Tôi cần một số gợi ý. Tôi muốn một cái gì đó như thế này:Tạo văn bản Đột quỵ cho iPhone không dây

alt text

EDITED:

UIFont *font = [UIFont fontWithName:@"Arial" size:222]; 
CGPoint point = CGPointMake(0,0); 

CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.7); 
CGContextSetRGBStrokeColor(context, 2, 2, 2, 1.0); 
CGContextSetTextDrawingMode(context, kCGTextFillStroke); 
CGContextSaveGState(context); 

// I change it to my outlet 
[label.text drawAtPoint:point withFont:font]; 

CGContextRestoreGState(context); 

Trả lời

21
UIFont *font = [UIFont fontWithName:@"Arial" size:14]; 
CGPoint point = CGPointMake(0,0); 

CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.7); 
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0); 
CGContextSetTextDrawingMode(context, kCGTextFillStroke); 
CGContextSaveGState(context); 
[@"Label" drawAtPoint:point withFont:font]; 

CGContextRestoreGState(context); 

bạn có thể tìm ở đây:

http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_text/dq_text.html

và trong các mã ví dụ ở đây: http://developer.apple.com/library/ios/#samplecode/QuartzDemo/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40007531

+0

cảm ơn bạn nhưng không có gì thay đổi! ... là "Nhãn" ổ cắm của tôi? xin lỗi Iam người mới trong lập trình IOS, tôi đặt mã của tôi trên viewDidLoad –

+0

bạn có thể gửi mã của bạn? – shannoga

+0

@ "Lable" chỉ là một Chuỗi, văn bản bạn muốn in – FoJjen

0

Vâng, nếu bạn muốn vẽ một văn bản trong một nhãn với phông chữ tùy chỉnh, như CGFontRef, nó không phải là khá đơn giản. Tôi đã googled một chút và tìm thấy một giải pháp cho bạn mà ngụ ý bạn kế thừa lớp UILabel và viết lại phương thức drawTextInRect cho việc này. Tất cả thông tin cần thiết cho bạn là here.

1

Tôi có một giải pháp làm sạch thực hiện UILabelStroke lớp con:

@implementation UILabelStroked 
@synthesize strokeColor; 
- (void)drawTextInRect:(CGRect)rect { 

    UIColor *borderColor = self.strokeColor; 
    UIColor *fillColor = self.textColor; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetLineWidth(context, 1.0f); 
    CGContextSetTextDrawingMode(context, kCGTextStroke); 
    self.textColor = borderColor; 
    [super drawTextInRect:rect]; 

    CGContextSetLineWidth(context, 0.0f); 
    CGContextSetTextDrawingMode(context, kCGTextFillStroke); 
    self.textColor = fillColor; 
    [super drawTextInRect:rect]; 
} 
@end 
Các vấn đề liên quan