Ios4 带有轮廓或笔划的标签

Ios4 带有轮廓或笔划的标签,ios4,uilabel,iphone,Ios4,Uilabel,Iphone,我想知道如何为UILabel创建文本笔划?有什么可能的办法吗? 谢谢, #import <Foundation/Foundation.h> @interface CustomLabel : UILabel { } @end #import "CustomLabel.h" @implementation CustomLabel - (void)drawTextInRect:(CGRect)rect { CGSize shadowOffset = self

我想知道如何为UILabel创建文本笔划?有什么可能的办法吗?

谢谢,

#import <Foundation/Foundation.h>


@interface CustomLabel : UILabel {

 }

 @end

#import "CustomLabel.h"


@implementation CustomLabel

- (void)drawTextInRect:(CGRect)rect {

    CGSize shadowOffset = self.shadowOffset;
    UIColor *textColor = self.textColor;

    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(c, 22);

    CGContextSetTextDrawingMode(c, kCGTextStroke);
    self.textColor = [UIColor whiteColor];
    [super drawTextInRect:rect];

    CGContextSetTextDrawingMode(c, kCGTextFill);
    self.textColor = textColor;
    self.shadowOffset = CGSizeMake(0, 0);
    [super drawTextInRect:rect];

    self.shadowOffset = shadowOffset;
    //works fine with no warning 
}   

根据字体和字符,添加以下内容可能对某些人有所帮助:

CGContextSetLineJoin(c,kCGLineJoinRound);

可以防止对过于尖锐的字符应用过大的笔划产生伪影。

此实现存在一个问题。绘制带有笔划的文本与绘制不带笔划的文本的字符图示符宽度稍有不同,这会产生“不居中”的结果。您可以通过在填充文字周围添加不可见的笔划来修复此问题

您应该替换:

CGContextSetTextDrawingMode(c, kCGTextFill);
self.textColor = textColor;
self.shadowOffset = CGSizeMake(0, 0);
[super drawTextInRect:rect];
与:

我不是100%确定,如果这是真的,因为我不知道如果
self.textColor=textColor
[textColor setFill]
具有相同的效果,但您可以理解

披露:我是THLabel的开发者


我不久前发布了一个UILabel子类,它允许在文本和其他效果中显示大纲。你可以在这里找到它:

为什么不尝试在Interface Builder中创建
标签
,并建立连接,这样你的应用程序就可以完成最少的工作?谢谢:)我很困惑:D现在可以工作了,谢谢!好主意!但它仍然需要参数化,比如线宽、颜色等。
CGContextSetTextDrawingMode(c, kCGTextFill);
self.textColor = textColor;
self.shadowOffset = CGSizeMake(0, 0);
[super drawTextInRect:rect];
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
self.textColor = textColor;
[[UIColor clearColor] setStroke]; // invisible stroke
self.shadowOffset = CGSizeMake(0, 0);
[super drawTextInRect:rect];