【问题标题】:Create text Stroke for UILabel iphone为 UILabel iphone 创建文本 Stroke
【发布时间】:2011-01-12 07:39:57
【问题描述】:

我想知道如何在 iOS4 中为 UILabel 创建文本笔划?我需要一些建议。我想要这样的东西:

已编辑:

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);

【问题讨论】:

  • 好吧,如果你想在一个自定义字体的标签中绘制一个文本,比如CGFontRef,这不是很简单。我用谷歌搜索了一下,为你找到了一个解决方案,这意味着你要继承 UILabel 类并为此重写 drawTextInRect 方法。您的所有必要信息是here

标签: iphone ios4


【解决方案1】:
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);

你可以看这里:

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

并在此处的示例代码中: http://developer.apple.com/library/ios/#samplecode/QuartzDemo/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40007531

【讨论】:

  • 谢谢,但没有任何改变! ...@“标签”是我的出路吗?对不起,我是IOS编程的新手,我把我的代码放在viewDidLoad上
  • @"Lable" 只是一个String,你要打印的文字
  • @Mc.Lover:这不正确,意思是为什么你被接受这个答案??
  • 这很好用。但是,CGContextSaveCGState(context); 行位于错误的位置。它应该在CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.7); 之前。否则,所有其他文本也将应用描边,因为恢复的状态是包含描边和填充颜色的状态。
【解决方案2】:

我有一个实现 UILabelStroke 子类的干净解决方案:

@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

【讨论】:

    猜你喜欢
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多