iphone上一些常用控件代码实现方式(原创)

本人在开发过程中自己写的控件一般生成代码

UIkitElements.h

//
//  UIkitElements.h
//  DigitGolf
//
//  Created by suruiqang on 8/5/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//
#pragma once
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface UIkitElements : NSObject {

}
+(UIButton*)createButton:(CGRect)rect 
		 withNormalImage:(NSString*)normalState 
		   SelectedImage:(NSString*)selectedState 
				   title:(NSString*)titleStr 
				 withTag:(NSInteger)tag;

+(UILabel*)createLable:(CGRect)rect 
			 withTitle:(NSString*)title 
			titleColor:(UIColor*)color 
				  Font:(UIFont*)font 
		 textAlignment:(UITextAlignment)alignmentType;

+(UITextField*)createTextField:(CGRect)rect 
				   borderStyle:(UITextBorderStyle)borderStyle 
					   keyType:(UIKeyboardType)keyType
					   holdStr:(NSString*)holderStr 
					 returnKey:(UIReturnKeyType)returnType clearButtonMode:(UITextFieldViewMode)mode ;

+(UIProgressView*)createProgressView:(CGRect)rect
						   viewStyle:(UIProgressViewStyle)style
					   progressValue:(float)value;
@end

#import "UIkitElements.h"

#define PACKAGE_FILE_PATH(FILE_NAME) [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:FILE_NAME]
@implementation UIkitElements

#pragma mark -
#pragma mark *************create a button  *************
+(UIButton*)createButton:(CGRect)rect
		 withNormalImage:(NSString*)normalState
		   SelectedImage:(NSString*)selectedState
				   title:(NSString*)titleStr
				 withTag:(NSInteger)tag
{
	UIButton* btn = [[UIButton alloc] initWithFrame:rect];
	[btn setBackgroundImage:[UIImage imageWithContentsOfFile:PACKAGE_FILE_PATH(normalState)] forState:UIControlStateNormal];
	[btn setBackgroundImage:[UIImage imageWithContentsOfFile:PACKAGE_FILE_PATH(selectedState)] forState:UIControlStateHighlighted];
[btn addTarget:self action:@selector(btnWasPressed:) forControlEvents:UIControlEventTouchUpInside];
			btn.showsTouchWhenHighlighted = YES;
			btn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin;
	if(titleStr!=nil)
	{
		[btn setTitle:titleStr forState:UIControlStateNormal];
	}
	btn.tag = tag;
	return btn;
}

#pragma mark -
#pragma mark *************create a lable  *************
+(UILabel*)createLable:(CGRect)rect 
			 withTitle:(NSString*)title 
			titleColor:(UIColor*)color 
				  Font:(UIFont*)font 
		 textAlignment:(UITextAlignment)alignmentType
{
	UILabel* lable = [[UILabel alloc] initWithFrame:rect];
	[lable setText:title];
	//[lable setTextColor:color];
	[lable setFont:font];
	[lable setTextAlignment:alignmentType];
	lable.backgroundColor = [UIColor clearColor];
	
	return lable;
}

#pragma mark -
#pragma mark *************create a textfield  *************
+(UITextField*)createTextField:(CGRect)rect 
				   borderStyle:(UITextBorderStyle)borderStyle 
					   keyType:(UIKeyboardType)keyType
					   holdStr:(NSString*)holderStr 
					 returnKey:(UIReturnKeyType)returnType clearButtonMode:(UITextFieldViewMode)mode
{
	UITextField* textField = [[UITextField alloc] initWithFrame:rect];
	textField.borderStyle = borderStyle;
	textField.keyboardType = keyType;
	textField.placeholder = holderStr;
	textField.returnKeyType = returnType;
	textField.clearButtonMode = mode;
	return textField;
}

#pragma mark -
#pragma mark *************create a ProgressView  *************
+(UIProgressView*)createProgressView:(CGRect)rect
						   viewStyle:(UIProgressViewStyle)style
					   progressValue:(float)value
{
	UIProgressView* progress = [[UIProgressView alloc] initWithFrame:rect];
	progress.progressViewStyle = UIProgressViewStyleBar;
	progress.progress = value;
	
	return progress;
}
@end

以后逐渐补充。。。。

原文地址:https://www.cnblogs.com/moshengren/p/1855247.html