在UIViewController
中有一个UITextView
,要对UITextView插入多个复选框。
怎么创建多个复选框,并且对这些复选框动态的创建方法?
目前代码如下:
-(void)Check
{
CGPoint origin = note.frame.origin;
NSString* head = [note.text substringToIndex:note.selectedRange.location];
CGSize initialSize = [head sizeWithFont:note.font constrainedToSize:note.contentSize];
NSUInteger startOfLine = [head length];
NSString* tail = [head substringFromIndex:startOfLine];
CGSize lineSize = [tail sizeWithFont:note.font forWidth:note.contentSize.width lineBreakMode:UILineBreakModeWordWrap];
CGPoint cursor = origin;
cursor.x += lineSize.width+15;
cursor.y += initialSize.height - lineSize.height-130;
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(cursor.x,cursor.y,15,15)];
[checkbox setBackgroundImage:[UIImage imageNamed:@"unchk.png"]forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:@"chk.png"]forState:UIControlStateSelected];
[checkbox setBackgroundImage:[UIImage imageNamed:@"chk.png"]forState:UIControlStateHighlighted];
checkbox.adjustsImageWhenHighlighted=YES;
[checkbox addTarget:self action:@selector(ChkUnChk) forControlEvents:UIControlEventTouchUpInside];
[note addSubview:checkbox];
}
-(void)ChkUnChk
{
if(checkUnCheck==NO)
{
[checkbox setBackgroundImage:[UIImage imageNamed:@"chk.png"]forState:UIControlStateNormal];
checkUnCheck=YES;
}
else if(checkUnCheck==YES)
{
[checkbox setBackgroundImage:[UIImage imageNamed:@"unchk.png"]forState:UIControlStateNormal];
checkUnCheck=NO;
}
}
-(void)checkboxSelected:(id)sender
{
checkBoxSelected = !checkBoxSelected;
[checkbox setSelected:checkBoxSelected];
}
其中note --> UITextView,checkbox --> UIButton