Custom CheckBox control developed for IOS

Source: Internet
Author: User

IOS does not have the system's CheckBox component, but it is often used in actual development. Therefore, a CheckBox control is specially written and the code is directly written.

:

The UICheckBoxButton. h file is as follows:

# Import

# Import "Common. h"

@ Interface UICheckBoxButton: UIControl

{

UILabel * label;

UIImageView * icon;

BOOL checked;

Id delegate;

}

@ Property (retain, nonatomic) id delegate;

@ Property (retain, nonatomic) UILabel * label;

@ Property (retain, nonatomic) UIImageView * icon;

-(BOOL) isChecked;

-(Void) setChecked: (BOOL) flag;

@ End

The UICheckBoxButton. m file is as follows:

# Import "UICheckBoxButton. h"

@ Implementation UICheckBoxButton

@ Synthesize label, icon, delegate;

-(Id) initWithFrame :( CGRect) frame {

If (self = [super initWithFrame: frame])

{

Icon = [[UIImageView alloc] initWithFrame: CGRectMake (0, 0, frame. size. height, frame. size. height)];

[Self setChecked: NO];

[Self addSubview: icon];

Label = [[UILabel alloc] initWithFrame: CGRectMake (icon. frame. size. width + 7, 0,

Frame. size. width-icon. frame. size. width-10,

Frame. size. height)];

Label. backgroundColor = [UIColor clearColor];

Label. textAlignment = UITextAlignmentLeft;

[Self addSubview: label];

[Self addTarget: self action: @ selector (clicked) forControlEvents: UIControlEventTouchUpInside];

}

Return self;

}

-(BOOL) isChecked {

Return checked;

}

-(Void) setChecked: (BOOL) flag {

If (flag! = Checked)

{

Checked = flag;

}

If (checked)

{

[Icon setImage: [UIImage imageNamed: @ "checkBoxSelect.png"];

}

Else

{

[Icon setImage: [UIImage imageNamed: @ "checkBoxNoSelect.png"];

}

}

-(Void) clicked {

[Self setChecked :! Checked];

If (delegate! = Nil)

{

SEL sel = NSSelectorFromString (@ "checkButtonClicked ");

If ([delegate respondsToSelector: sel])

{

[Delegate initialize mselector: sel];

}

}

}

-(Void) dealloc {

Delegate = nil;

[Label release];

[Icon release];

[Super dealloc];

}

@ End

Usage:

UICheckBoxButton * checkBoxButton = [[UICheckBoxButton alloc] initWithFrame: CGRectMake (30, 50,220, 25)];

CheckBoxButton. delegate = self. delegate;

CheckBoxButton. label. text = [Common getTextByTag: @ "noCostPrompt"];

CheckBoxButton. label. textColor = [Common getColorByTag: @ "alertLabelcolor"];

[Self. view addSubview: checkBoxButton];

[CheckBoxButton release];

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.