将内阴影应用于 UILabel
我想将内阴影应用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
借用上面鲁本的答案,如果你做相反的事情,即。将文本颜色设置为等于背景颜色(alpha 较低),然后将阴影设置为更强的颜色,它会创建不错的插图效果。这就是我的意思(注意:我的视图背景是白色):
这是输出
这可能只会在非常浅的背景上工作,因为我怀疑它会在较暗的背景上产生不需要的覆盖。
您当然可以改变 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):
and this is the output
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.
我尝试这样做,但最终选择使用默认的
shadowOffset
并使用shadowColor
为文本提供内部阴影效果。在小文本中,它可以为您提供良好的内阴影效果。例如,如果您有灰色背景并将白色应用于阴影,则您将获得可接受的内部阴影效果。有时,最好使用图形工具设计这些文本,并根据需要制作本地化副本。
I tried to do this but finally opted to use the default
shadowOffset
and play with theshadowColor
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.
在这里回答: UILabel 中的内阴影 代码很长,但似乎可以工作
Answer here : Inner Shadow in UILabel Long code but it seems to work