I am trying to draw a char array on a UIView. The problem is that the view is not refreshing automatically and hence the chars are not displayed. When we touch the screen here and there then it gets displayed (sometime only a portion of the char). When we are redrawing new set of chars (when array is updated) the old one doesn't gets cleared as well. While redrawing the updated array previously drawn doesn't get cleared from UIView, it just over writes on same view and displays both.
Here is my code:
In MyView Class.
-(void)drawRect:(CGRect)rect
{
context=UIGraphicsGetCurrentContext();
}
-(void)initializeWithChar:(char)characterToDisplay andAttribute:(Byte)attribute
{
displayChar = characterToDisplay;
displayCharAttribute = attribute;
}
-(void)setNeedsDisplayInRect:(CGRect)rect{
NSString *charString;
UIGraphicsPushContext(context);
charString = [NSString stringWithFormat:@"%c",displayChar];
// while background color is black.
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
[charString drawInRect:CGContextConvertRectToUserSpace(context, rect)
withFont:[UIFont fontWithName:@"Courier New" size:10]
lineBreakMode:NSLineBreakByWordWrapping
alignment:NSTextAlignmentLeft];
UIGraphicsPopContext();
}
-(void)clearScreenInRect:(CGRect) rect{
[self.viewForBaselineLayout setClearsContextBeforeDrawing:YES];
[self.viewForBaselineLayout setOpaque :NO];
CGContextClearRect (context,rect);
// I TRIED THIS AS WELL -> but dosen't work..
//CGContextSetFillColorWithColor(contxt, [UIColor blackColor].CGColor);
//CGContextFillRect(contxt, rect) ;
}
writeDrawView class
// CODE BELOW is DRAWING FRESH ARRAY ON VIEW
//this method draw a sign in form on view (content are placed in SignInFormArray)
-(void)writeToDrawViewFrom_SignInFormArray{
for (int rownumber = 0; rownumber < 10; rownumber++) {
for (int columnNumber = 0; columnNumber < 30; columnNumber++) {
[myView initializeWithChar:SignInFormArray[rownumber][columnNumber] andAttribute:Attribute[rownumber][columnNumber]];
[myView setNeedsDisplayInRect:CGRectMake(columnNumber*12 , rownumber*30, 12, 30)];
}
}
}
-(void)clearButtonClicked{
for (int rownumber = 0; rownumber < 10; rownumber++) {
for (int columnNumber = 0; columnNumber < 30; columnNumber++) {
[myView clearScreenInRect:CGRectMake(columnNumber*12 , rownumber *30 , 12, 30)];
}
}
}
// CODE BELOW is UPDATING VIEW FROM UPDATED ARRAY
//this method draw a home page menu form on view (content are placed in HomePageMenuArray)
-(void)writeToDrawViewFrom_HomePageMenuArray{
for (int rownumber = 0; rownumber < 10; rownumber++) {
for (int columnNumber = 0; columnNumber < 30; columnNumber++) {
[myView initializeWithChar:HomePageMenuArray[rownumber][columnNumber] andAttribute:Attribute[rownumber][columnNumber]];
[myView setNeedsDisplayInRect:CGRectMake(columnNumber*12 , rownumber*30, 12, 30)];
}
}
}