【问题标题】:Make view transparent without seeing the text from bottom view使视图透明而不从底部视图中看到文本
【发布时间】:2012-03-20 20:32:14
【问题描述】:

在 iPhone 日历应用程序中,如果您有 2 个相互重叠的图块,则底部图块中的文本会被截断,并且无法通过顶部透明图块看到。我如何才能保持图块透明但不让底部图块中的文本显示到顶部图块?

这是我的代码

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);
    }
}

我希望这张照片中的文字不会像第二张照片那样显示在前面的图块中。

【问题讨论】:

  • 如果您要 -1 并投票关闭,请至少发布原因。

标签: iphone objective-c ios5 uiview


【解决方案1】:

@user1272965 是正确的,因为每个图块都需要知道其上方的图块,然后您可以通过将图块视图设为自定义子类并手动实现其绘图来获得您正在寻找的文本效果:

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

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

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

在你的 tile 视图的 drawRect 中,绘制 tile,然后绘制它上面的视图剪切的文本:

// 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)];

【讨论】:

  • 这听起来可行。有时间我会试试的。
【解决方案2】:

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

您可以使用UILabel 的框架属性以及最前面日历约会的frame 属性为此UIView 创建frame

【讨论】:

  • UIView 必须是透明的,这样你才能看到它们下面的小时线。
【解决方案3】:

我认为本机日历应用会压缩与您在同一开始时间出现的图块,但它也会压缩时间重叠的图块,即如果后一个图块的开始时间介于较早的瓷砖。

但是,如果您需要您的 UI 将这些图块重叠绘制,那么就没有办法了:被遮盖的图块需要知道它在下面(从之后开始并重叠),并且它需要隐藏其标签,或者减少它们的标签阿尔法级别。

【讨论】:

  • 隐藏标签并不是 Apple 的做法。从我的屏幕截图中可以看出,您可以看到字母的上半部分。如果有的话,Apple 只是更改了标签大小以匹配前面瓷砖的顶部。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多