写入矩形并用颜色填充

时间:2014-03-12 03:19:42

标签: ios objective-c uicolor

我想绘制一个矩形,用调色板中的所选颜色填充矩形然后写入其中。我能够绘制并写入它但是当我填充矩形时,书面文本被覆盖。我看不到任何东西。 我正在开发IOS中的扫雷游戏并为打开和关闭的单元格设置单元格颜色。

        UIFont *font = [UIFont systemFontOfSize: .75*self.dh];
        for(int i = 0 ; i < 16; i ++){
            for(int j = 0;j < 16;j ++){
                if(mineMatrix[i][j] == -2 || mineMatrix[i][j] == -4){
                    //                    NSString *mine = @"F";
                    //                    CGPoint xy = { (i+0.17)*self.dw, j*self.dh};
                    //                    [mine drawAtPoint: xy withFont: font];


                    // establish bounding box for image
                    CGPoint tl = { (i)*self.dw, j*self.dh + 103};
                    CGRect imageRect = CGRectMake(tl.x, tl.y, dw, dh);

                    // place appropriate image where dragging stopped
                    UIImage *img;
                    img = [UIImage imageNamed:@"flags.png"];
                    [img drawInRect:imageRect];


                }else{
                    CGPoint tl = { (i)*self.dw, j*self.dh + 103};
                    CGRect imageRect = CGRectMake(tl.x, tl.y, dw, dh);
                    CGContextRef context = UIGraphicsGetCurrentContext();
                    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
                    CGContextFillRect(context, imageRect);
                }
                if(mineMatrix[i][j] != -3 && mineMatrix[i][j] != 0 && mineMatrix[i][j] != -2 && mineMatrix[i][j] != -1 && mineMatrix[i][j] != -4){
                    NSString *mine = [[NSNumber numberWithInt: mineMatrix[i][j]] stringValue];
                    CGPoint xy = { (i+0.17)*self.dw, j*self.dh + 103};
                    [mine drawAtPoint: xy withFont: font];
                }
                if(mineMatrix[i][j] == 0 || mineMatrix[i][j] == -1){
                    UIImage *img;
                    CGPoint tl = { (i)*self.dw, j*self.dh + 103};
                    CGRect imageRect = CGRectMake(tl.x, tl.y, dw, dh);
                    [img drawInRect:imageRect];
                }


            }

        }

    }

2 个答案:

答案 0 :(得分:2)

问题在于,在绘制文本时,您已经设置了上下文的填充颜色以绘制背景。因此,文本以与背景相同的颜色绘制,您看不到任何内容。

因此,只需在绘制文本之前将填充颜色更改为其他颜色,如下所示:

[[UIColor blackColor] set]; // or whatever color you like
[mine drawAtPoint: xy withFont: font];

或者,更好的是,绘制具有自己颜色的属性文本。

答案 1 :(得分:0)

您是否考虑为每个单元格创建UIView的子类。由于您正在创建一个扫雷游戏,我相信您的每个单元都需要具有以下功能:

  1. 有2个州 - 开放和关闭
  2. 回应触摸 - 标记或打开
  3. 有一个标签 - 代表地雷数量
  4. 有图像 - 如果是地雷或国旗
  5. 能够更改矩形颜色
  6. 所有这些都可以通过UIView方法实现。例如你的exampleMine.h:

    #import <UIKit/UIKit.h>
    
    @interface exampleMine :UIView{
        UIImageView *backgroundImage; //used for either flags or mines
        UIButton* cellButton; //used for flagging or opening actions
        UILabel* numMineLabel;
        BOOL isMine, isOpen, isFlagged;
        int numSurroundingMines;
    }
    

    在你的exampleMine.m中有以下功能

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if(self){
            backgroundImage = [[UIImageView alloc] initWithFrame: self.bounds];
            backgroundImage.hidden = YES; //You don't need this image to be shown at the start
            backgroundImage.backgroundColor = [UIColor clearColor];
            numMineLabel = [[UILabel alloc] initWithFrame: self.bounds];
            numMineLabel.hidden = YES; //You don't need this label at the start either
            numMineLabel.textAlignment = NSTextAlignmentCenter;
            numMineLabel.backgroundColor = [UIColor clearColor];
            cellButton = [UIButton buttonWithType: UIButtonTypeCustom];//you can add a target and selector for touch responses in your main view
            cellButton.frame = self.bounds;
            cellButton.backgroundColor = [UIColor clearColor];
            cellButton.enabled = YES;
            cellButton.hidden = NO:
            [self addSubview: backgroundImage];
            [self addSubview: numMineLabel];
            [self addSubview: cellButton];
            self.backgroundColor = [UIColor blueColor];//I've set blueColor as default for closed cell - you can choose any color
            isMine = isOpen = isFlagged = NO;
        }
        return self;
    }
    

    这是初始化一个新单元格。以下功能有助于设置其余细节。

    - (void) setMine{ //Called only if given cell is a mine
        isMine = YES;
    }
    
    - (void) setNumSurroundingMines: (int) numSurrMines{
        numSurroundingMines = numSurrMines;
        numMineLabel.text = [NSString stringWithFormat: @"%d", numSurroundingMines];
    }
    
    - (void) setOpen{
        if(isMine){
            backgroundImage.image = [UIImage imageNamed: @"mine.png"]; //or whichever image you would use
            backgroundImage.hidden = NO;
            self.backgroundColor = [UIColor redColor];
        }
        else{
            numMineLabel.hidden = NO;
        }
        cellButton.hidden = YES; //You would not need the button any longer
        isOpen = YES;
    }
    
    - (void) setFlagged{
        if(isFlagged){
            backgroundImage.hidden = YES;
        }
        else{
            backgroundImage.image = [UIImage imageNamed: @"flags.png"]; //or whichever image you would use
            backgroundImage.hidden = NO;
        }
    }
    

    我意识到这是一个很长的答案,它可能无法直接回答你的问题。但是,它可能是解决问题的另一种方法。我曾经尝试过这种方法,一旦使用java swing,它运行得非常好,并且使事件处理非常容易。