带有轮廓或描边的 uilabel
我想知道如何为 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 ?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据字体和字符添加这一点可能会对某些人有所帮助,添加:
可以防止因对过于尖锐的字符应用过大的笔画而产生的伪影。
It may be helpful to some to add that depending on font and characters, adding:
can prevent artifacts from too large a stroke applied to too sharp a character.
此实施有一个问题。使用描边绘制文本的字符字形宽度与绘制不使用描边的文本的字符字形宽度略有不同,这可能会产生“不居中”的结果。您可以通过在填充文本周围添加不可见的笔划来解决此问题。
您应该将: 替换
为:
我不是 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:
with:
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