将内阴影应用于 UILabel

发布于 2024-11-03 16:35:23 字数 828 浏览 7 评论 0原文

我想将内阴影应用于 UILabel。我有一个解决方案,但还不够好。有人有更好的解决方案吗?

// UILabel subclass

- (void) drawTextInRect:(CGRect)rect {
    CGSize myShadowOffset = CGSizeMake(0, 2);
    float myColorValues[] = {255, 0, 0, 1};

    CGContextRef myContext = UIGraphicsGetCurrentContext();
    CGContextSaveGState(myContext);

    CGColorSpaceRef myColorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef myColor = CGColorCreate(myColorSpace, myColorValues);

    CGContextSetShadowWithColor (myContext, myShadowOffset, 5, myColor);
    CGContextSetBlendMode(myContext, kCGBlendModeLighten);

    [super drawTextInRect:rect];

    CGColorRelease(myColor);
    CGColorSpaceRelease(myColorSpace); 

    CGContextRestoreGState(myContext);
}

我熟悉 UILabel 的图层属性,但是 shadow offset 为我们提供了外部阴影,而不是内部阴影(除非我遗漏了一些东西)。

I want to apply inner-shadow to a UILabel. I have a solution, but it's not good enough. Anyone with a better solution?

// UILabel subclass

- (void) drawTextInRect:(CGRect)rect {
    CGSize myShadowOffset = CGSizeMake(0, 2);
    float myColorValues[] = {255, 0, 0, 1};

    CGContextRef myContext = UIGraphicsGetCurrentContext();
    CGContextSaveGState(myContext);

    CGColorSpaceRef myColorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef myColor = CGColorCreate(myColorSpace, myColorValues);

    CGContextSetShadowWithColor (myContext, myShadowOffset, 5, myColor);
    CGContextSetBlendMode(myContext, kCGBlendModeLighten);

    [super drawTextInRect:rect];

    CGColorRelease(myColor);
    CGColorSpaceRelease(myColorSpace); 

    CGContextRestoreGState(myContext);
}

I'm familiar with the layer property of UILabel, but shadow offset gives us a outer-shadow, NOT inner-shadow (unless i'm missing something).

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

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

发布评论

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

评论(3

雨后咖啡店 2024-11-10 16:35:23

借用上面鲁本的答案,如果你做相反的事情,即。将文本颜色设置为等于背景颜色(alpha 较低),然后将阴影设置为更强的颜色,它会创建不错的插图效果。这就是我的意思(注意:我的视图背景是白色):

cell.textLabel.textColor     = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5];
cell.textLabel.shadowColor   = [UIColor darkGrayColor];
cell.textLabel.shadowOffset  = CGSizeMake(-1.0,-1.0);
[cell.textLabel setText:@"Welcome to MyApp!"];

这是输出

inset UILabel text

这可能只会在非常浅的背景上工作,因为我怀疑它会在较暗的背景上产生不需要的覆盖。

您当然可以改变 ShadowOffset 来改变光线的方向。

Borrowing on Ruben's answer above, if you do the reverse ie. set your text color equal to your background color (with low alpha) and then set the shadow to be a stronger color, it creates a decent inset effect. Here's what I mean (Note: My view background is white):

cell.textLabel.textColor     = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5];
cell.textLabel.shadowColor   = [UIColor darkGrayColor];
cell.textLabel.shadowOffset  = CGSizeMake(-1.0,-1.0);
[cell.textLabel setText:@"Welcome to MyApp!"];

and this is the output

inset UILabel text

This would probably only work on very light backgrounds as I suspect it will create unwanted overlay on darker backgrounds.

You can ofcourse vary the shadowOffset to change the direction of light.

究竟谁懂我的在乎 2024-11-10 16:35:23

我尝试这样做,但最终选择使用默认的 shadowOffset 并使用 shadowColor 为文本提供内部阴影效果。在小文本中,它可以为您提供良好的内阴影效果。例如,如果您有灰色背景并将白色应用于阴影,则您将获得可接受的内部阴影效果。

有时,最好使用图形工具设计这些文本,并根据需要制作本地化副本。

I tried to do this but finally opted to use the default shadowOffset and play with the shadowColor to give the inner drop shadow effect to the text. In small texts it gives you a good inner shadow effect. For example, if you have a grayColor background and apply a whiteColor to the shadow, then you have an acceptable inner shadow effect.

Sometimes, it's better to design those texts with graphic tools and make localized copies if needed.

Oo萌小芽oO 2024-11-10 16:35:23

在这里回答: UILabel 中的内阴影 代码很长,但似乎可以工作

Answer here : Inner Shadow in UILabel Long code but it seems to work

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