iphone - 将内阴影应用到 UILabel

标签 iphone ios ipad uilabel shadow

<分区>

我想将内阴影应用于 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 给了我们一个外部阴影,而不是内部阴影(除非我遗漏了什么)。

最佳答案

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

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 来改变光的方向。

关于iphone - 将内阴影应用到 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5817330/

相关文章:

iphone - Type0 字体的 PDF 解析给我带来麻烦(IOS 应用程序)?

iphone - NSData getBytes 在模拟器中提供与在设备上不同的结果

ios - 使用 Swift 5.0 隐藏 HIChart 饼图的数据标签

iPhone游戏编程: Multiple UIImageViews in a UIViewController or UIImages in a UIView?

iphone - iOS4 图像中哪个 ppi 是正确的(正常和高分辨率)

ios - MonoTouch试用版已提交给App Store?

iphone - 可以在我的应用程序中将多个类的默认值注册到 NSUserDefaults 吗?

ios - NSURLSession 后台下载不工作

iphone - iPhone/Android 浏览器是否支持 CSS @media 手持设备?

iphone - 在游戏中调用 addObject 时出现 IOS 8 XCode 6 beta 4 异常