繁体   English   中英

将内部阴影应用于 UILabel [重复]

[英]Apply inner-shadow to UILabel [duplicate]

这个问题在这里已经有了答案:

我想将内部阴影应用于 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 的 layer 属性,但是shadow offset给了我们一个外阴影,而不是内阴影(除非我错过了一些东西)。

借用上面鲁本的回答,如果你做相反的事情,即。 将您的文本颜色设置为等于您的背景颜色(具有低 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!"];

这是 output

插入 UILabel 文本

这可能只适用于非常浅的背景,因为我怀疑它会在较暗的背景上产生不需要的叠加。

你当然可以改变 shadowOffset 来改变光的方向。

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

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

在这里回答: UILabel Long 代码中的内阴影,但它似乎有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM