带有轮廓或描边的 uilabel

发布于 2024-10-19 00:46:52 字数 1012 浏览 1 评论 0原文

我想知道如何为 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 
}   

现在的问题是我如何在不同的视图控制器上使用带有 IBOutlet 标签的这个子类。对吗:

    label = [[CustomLabel alloc] initWithFrame:CGRectMake(0, 0, 190, 190)];

I was wondering how can I create text stroke for UILabel ? is there any possible way ?
enter image description here

thank you ,

#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 
}   

now the question is how can i use this subclass with a IBOutlet label on different viewcontrollers . is it right :

    label = [[CustomLabel alloc] initWithFrame:CGRectMake(0, 0, 190, 190)];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹梦 2024-10-26 00:46:52

根据字体和字符添加这一点可能会对某些人有所帮助,添加:

CGContextSetLineJoin(c,kCGLineJoinRound);

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

It may be helpful to some to add that depending on font and characters, adding:

CGContextSetLineJoin(c,kCGLineJoinRound);

can prevent artifacts from too large a stroke applied to too sharp a character.

送你一个梦 2024-10-26 00:46:52

此实施有​​一个问题。使用描边绘制文本的字符字形宽度与绘制不使用描边的文本的字符字形宽度略有不同,这可能会产生“不居中”的结果。您可以通过在填充文本周围添加不​​可见的笔划来解决此问题。

您应该将: 替换

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

我不是 100% 确定,如果这是真的,因为我不知道 self.textColor = textColor; 是否与 [textColor 具有相同的效果setFill],但你明白了。

披露:我是 THLabel 的开发者。

我不久前发布了一个 UILabel 子类,它允许文本中的轮廓和其他效果。您可以在这里找到它:https://github.com/tobihagemann/THLabel

There is one issue with this implementation. Drawing a text with stroke has a slightly different character glyph width than drawing a text without stroke, which can produce "uncentered" results. You can fix that by adding an invisible stroke around the fill text.

You should replace:

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

with:

CGContextSetTextDrawingMode(context, kCGTextFillStroke);
self.textColor = textColor;
[[UIColor clearColor] setStroke]; // invisible stroke
self.shadowOffset = CGSizeMake(0, 0);
[super drawTextInRect:rect];

I'm not 100% sure, if that's the real deal, because I don't know if self.textColor = textColor; has the same effect as [textColor setFill], but you get the idea.

Disclosure: I'm the developer of THLabel.

I've released a UILabel subclass a while ago, which allows an outline in text and other effects. You can find it here: https://github.com/tobihagemann/THLabel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文