使视图透明而不从底部视图中看到文本

时间:2012-03-20 20:32:15

标签: iphone objective-c ios5 uiview

在iPhone日历应用程序中,如果您有两个相互重叠的瓷砖,则底部瓷砖的文字会被切掉,透过顶部透明瓷砖无法看到。如何让瓷砖保持透明但不会将底部瓷砖的文字显示到顶部瓷砖?

这是我的代码

APCalendarDayTile *tile = (APCalendarDayTile *)view;
CGFloat startPos = [APCalendarCurrentDayView yAxisForTime:[APCalendarCurrentDayView minutesToTime:tile.appointment.startDate]];
CGFloat endPos = [APCalendarCurrentDayView yAxisForTime:[APCalendarCurrentDayView minutesToTime:tile.appointment.endDate]];
tile.frame = CGRectMake(kLeftSideBuffer, startPos, (self.bounds.size.width - kLeftSideBuffer - kRightSideBuffer) , endPos - startPos);
tile.backgroundColor = [UIColor colorWithHexString:tile.appointment.appointmentColor]; <-- This also sets the alpha that makes it transparent.
tile.layer.borderColor = [UIColor colorWithHexString:tile.appointment.appointmentColor alpha:1.0].CGColor;
tile.layer.borderWidth = 1.0f;

平铺代码

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        CALayer *layer = [self layer];
        [layer setMasksToBounds:YES];
        [layer setCornerRadius:kCornerRadius];
    }
    return self;
}

- (id)init
{
    if (self = [super init]) {
        self.clipsToBounds = YES;
        self.userInteractionEnabled = YES;
        self.multipleTouchEnabled = NO;

        tileTitle = [[UILabel alloc] init];
        tileTitle.textColor = [UIColor blackColor];
        tileTitle.backgroundColor = [UIColor clearColor];
        tileTitle.font = [UIFont boldSystemFontOfSize:13.0f];
        [tileTitle setAutoresizesSubviews:YES];
        [tileTitle setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];


        tileDescription = [[UILabel alloc] init];
        tileDescription.textColor = [UIColor blackColor];
        tileDescription.backgroundColor = [UIColor clearColor];
        tileDescription.font = [UIFont systemFontOfSize:11.0f];
        tileDescription.lineBreakMode = UILineBreakModeTailTruncation;
        [tileDescription setAutoresizesSubviews:YES];
        [tileDescription setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];


        [self setAutoresizesSubviews:YES];
        [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];


        [self addSubview:tileTitle];
        [self addSubview:tileDescription];
    }

    return self;
}

- (void)layoutSubviews
{
    CGRect myBounds = self.bounds;
    CGSize stringSize = [tileTitle.text sizeWithFont:tileTitle.font];

    if (myBounds.size.height <= 22.0) {
        tileTitle.frame = CGRectMake(3, 0, myBounds.size.width, stringSize.height);
        tileDescription.frame = CGRectMake(stringSize.width + 6, -1, myBounds.size.width, 14);
    } else {
        tileTitle.frame = CGRectMake(3, 0, myBounds.size.width, stringSize.height);
        tileDescription.frame = CGRectMake(5, tileTitle.frame.size.height, myBounds.size.width, 14);
    }
}

我希望这张照片中的文字不会像第二张照片一样显示在前面的瓷砖上。

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:0)

我认为原生日历应用程序会挤压在与您相同的开始时间发生的切片,但它也会挤压时间重叠的切片,即如​​果后一切片的开始时间介于切片的开始时间和结束时间之间。早期的瓷砖。

但是如果你需要你的UI来绘制这些瓷砖重叠,那么就没有办法了解它:模糊的瓷砖需要知道它在下面(开始和重叠),它需要隐藏它的标签,或者减少它们alpha级别。

答案 1 :(得分:0)

在两个方框之间添加UIView怎么样?这个中间UIView将覆盖字母(并且与后方事件具有相同的背景颜色),这可以解决您的问题:没有文字通过,但您会看到另一个标签背后的颜色!

您可以使用frame的frame属性以及最前面日历约会的UIView属性为此UILabel创建frame。< / p>

答案 2 :(得分:0)

@ user1272965是正确的,因为每个磁贴需要知道它上面有哪些磁贴,然后您可以通过使磁贴视图成为自定义子类并手动实现其绘图来获得您正在寻找的文本效果:

每个图块都需要访问其上方的图块(或至少是其框架):

NSInteger tileCount = [tilesAboveMine count];
CGRect framesAboveMine[tileCount];

for (int i=0; i<tileCount; i++) {
    framesAboveMine[i] = [tilesAboveMine objectAtIndex:i].frame;
}

在平铺视图的drawRect中,绘制平铺,然后绘制由其上方视图剪切的文本:

// draw aspects of the tile not to be clipped, like the background image.
// the setup for clipping:

CGContextClipToRects(context, framesAboveMine, tileCount);

然后绘制文本,该文本将被上面的框架剪切

[myCalendarString drawAtPoint:CGPointMake(10,10)];