
注:代码摘自苹果官方"UICatalog"源码:
1. UIbutton
UIbutton *detaildisclosurebuttonType = [[UIbuttonbuttonWithType:UIbuttonTypeDetaildisclosure]retain];
detaildisclosurebuttonType.frame =CGRectMake(250.0,8.0,25.0,25.0);
[detaildisclosurebuttonTypesetTitle:@"Detail disclosure"forState:UIControlStatenormal];
detaildisclosurebuttonType.backgroundcolor = [UIcolorclearcolor];
[detaildisclosurebuttonTypeaddTarget:selfaction:@selector(action:)forControlEvents:UIControlEventtouchUpInsIDe];
+ (UIbutton *)buttonWithTitle:(Nsstring *)Title target:(ID)target selector:(SEL)selector frame:(CGRect)frame
image:(UIImage *)image imagepressed:(UIImage *)imagepressed darkTextcolor:(BOol)darkTextcolor
{
UIbutton *button = [[UIbuttonalloc]initWithFrame:frame];
// or you can do this:
//UIbutton *button = [[UIbutton buttonWithType:UIbuttonTypeCustom] retain];
//button.frame = frame;
button.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment =UIControlContentHorizontalAlignmentCenter;
[button setTitle:Title forState:UIControlStatenormal];
if (darkTextcolor)
{
[buttonsetTitlecolor:[UIcolorblackcolor]forState:UIControlStatenormal];
}
else
{
[buttonsetTitlecolor:[UIcolorwhitecolor]forState:UIControlStatenormal];
}
UIImage *newImage = [imagestretchableImageWithleftCapWIDth:12.0topCapHeight:0.0];
[button setBackgroundImage:newImage forState:UIControlStatenormal];
UIImage *newpressedImage = [imagepressedstretchableImageWithleftCapWIDth:12.0topCapHeight:0.0];
[button setBackgroundImage:newpressedImage forState:UIControlStateHighlighted];
[button addTarget:target action:selector forControlEvents:UIControlEventtouchUpInsIDe];
// in case the parent vIEw draws with a custom color or gradIEnt,use a transparent color
button.backgroundcolor = [UIcolorclearcolor];
return button;
}
2. UISwitch
- (UISwitch *)switchCtl
{
if (switchCtl ==nil)
{
CGRect frame = CGRectMake(198.0,12.0, 94.0,27.0);
switchCtl = [[UISwitchalloc]initWithFrame:frame];
[switchCtladdTarget:selfaction:@selector(switchAction:)forControlEvents:UIControlEventValueChanged];
// in case the parent vIEw draws with a custom color or gradIEnt,use a transparent color
switchCtl.backgroundcolor = [UIcolorclearcolor];
[switchCtlsetAccessibilityLabel:NSLocalizedString(@"StandardSwitch",@"")];
switchCtl.tag =kVIEwTag;// tag this vIEw for later so we can remove it from recycled table cells
}
returnswitchCtl;
}
3. UiSlider
- (UiSlider *)slIDerCtl
{
if (slIDerCtl ==nil)
{
CGRect frame =CGRectMake(174.0,120.0,kSlIDerHeight);
slIDerCtl = [[UiSlideralloc]initWithFrame:frame];
[slIDerCtladdTarget:selfaction:@selector(slIDerAction:)forControlEvents:UIControlEventValueChanged];
// in case the parent vIEw draws with a custom color or gradIEnt,use a transparent color
slIDerCtl.backgroundcolor = [UIcolorclearcolor];
slIDerCtl.minimumValue =0.0;
slIDerCtl.maximumValue =100.0;
slIDerCtl.continuous =YES;
slIDerCtl.value =50.0;
// Add an accessibility label that describes the slIDer.
[slIDerCtlsetAccessibilityLabel:NSLocalizedString(@"StandardSlIDer",@"")];
slIDerCtl.tag =kVIEwTag;// tag this vIEw for later so we can remove it from recycled table cells
}
returnslIDerCtl;
}
4. UIPageControl
- (UIPageControl *)pageControl
{
if (pageControl ==nil)
{
CGRect frame = CGRectMake(120.0,14.0, 178.0,20.0);
pageControl = [[UIPageControlalloc]initWithFrame:frame];
[pageControladdTarget:selfaction:@selector(pageAction:)forControlEvents:UIControlEventtouchUpInsIDe];
// in case the parent vIEw draws with a custom color or gradIEnt,use a transparent color
pageControl.backgroundcolor = [UIcolorgraycolor];
pageControl.numberOfPages =10;// must be set or control won't draw
pageControl.tag =kVIEwTag;// tag this vIEw for later so we can remove it from recycled table cells
}
returnpageControl;
}
5. UIActivityIndicatorVIEw
- (UIActivityIndicatorVIEw *)progressInd
{
if (progressInd ==nil)
{
CGRect frame =CGRectMake(265.0,12.0,kProgressIndicatorSize,kProgressIndicatorSize);
progressInd = [[UIActivityIndicatorVIEwalloc]initWithFrame:frame];
[progressIndstartAnimating];
progressInd.activityIndicatorVIEwStyle =UIActivityIndicatorVIEwStyleGray;
[progressInd sizetoFit];
progressInd.autoresizingMask = (UIVIEwautoresizingFlexibleleftmargin |
UIVIEwautoresizingFlexibleRightmargin |
UIVIEwautoresizingFlexibletopmargin |
UIVIEwautoresizingFlexibleBottommargin);
progressInd.tag =kVIEwTag;// tag this vIEw for later so we can remove it from recycled table cells
}
returnprogressInd;
}
6. UIProgressVIEw
- (UIProgressVIEw *)progressbar
{
if (progressbar ==nil)
{
CGRect frame =CGRectMake(126.0,20.0,kuiprogressbarWIDth,kuiprogressbarHeight);
progressbar = [[UIProgressVIEwalloc]initWithFrame:frame];
progressbar.progressVIEwStyle =UIProgressVIEwStyleDefault;
progressbar.progress =0.5;
progressbar.tag =kVIEwTag;// tag this vIEw for later so we can remove it from recycled table cells
}
returnprogressbar;
}
7. UITextFIEld
- (UITextFIEld *)textFIEldnormal
{
if (textFIEldnormal ==nil)
{
CGRect frame =CGRectMake(kleftmargin,8.0,kTextFIElDWIDth,kTextFIEldHeight);
textFIEldnormal = [[UITextFIEldalloc]initWithFrame:frame];
textFIEldnormal.borderStyle =UITextborderStyleBezel;
textFIEldnormal.textcolor = [UIcolorblackcolor];
textFIEldnormal.Font = [UIFontsystemFontOfSize:17.0];
textFIEldnormal.placeholder =@"<enter text>";
textFIEldnormal.backgroundcolor = [UIcolorwhitecolor];
textFIEldnormal.autocorrectionType =UITextautocorrectionTypeNo;// no auto correction support
textFIEldnormal.keyboardType =UIKeyboardTypeDefault;// use the default type input method (entire keyboard)
textFIEldnormal.returnKeyType =UIReturnKeyDone;
textFIEldnormal.clearbuttonMode =UITextFIEldviewmodeWhileEditing;// has a clear 'x' button to the right
textFIEldnormal.tag =kVIEwTag;// tag this control so we can remove it later for recycled cells
textFIEldnormal.delegate =self;// let us be the delegate so we kNow when the keyboard's "Done" button is pressed
// Add an accessibility label that describes what the text fIEld is for.
[textFIEldnormalsetAccessibilityLabel:NSLocalizedString(@"normalTextFIEld",@"")];
}
returntextFIEldnormal;
}
8. UITextVIEw(UITextVIEwDelegate)
- (voID)setupTextVIEw
{
self.textVIEw = [[[UITextVIEwalloc]initWithFrame:self.vIEw.frame]autorelease];
self.textVIEw.textcolor = [UIcolorblackcolor];
self.textVIEw.Font = [UIFontFontWithname:@"Arial"size:18];
self.textVIEw.delegate =self;
self.textVIEw.backgroundcolor = [UIcolorwhitecolor];
self.textVIEw.text =@"Now is the time for all good developers to come to serve their country.\n\nNow is the time for all good developers to come to serve their country.";
self.textVIEw.returnKeyType =UIReturnKeyDefault;
self.textVIEw.keyboardType =UIKeyboardTypeDefault;// use the default type input method (entire keyboard)
self.textVIEw.scrollEnabled =YES;
// this will cause automatic vertical resize when the table is resized
self.textVIEw.autoresizingMask =UIVIEwautoresizingFlexibleHeight;
// note: for UITextVIEw,if you don't like autocompletion while tyPing use:
// myTextVIEw.autocorrectionType = UITextautocorrectionTypeNo;
[self.vIEwaddSubvIEw:self.textVIEw];
}
TextVIEw键盘处理:
- (voID)vIEwWillAppear:(BOol)animated
{
// Listen for keyboard hIDe/show notifications so we can properly adjust the table's height
[supervIEwWillAppear:animated];
[[NSNotificationCenterdefaultCenter]addobserver:selfselector:@selector(keyboarDWillShow:)name:UIKeyboarDWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]addobserver:selfselector:@selector(keyboarDWillHIDe:)name:UIKeyboarDWillHIDeNotificationobject:nil];
}
- (voID)vIEwDIDdisappear:(BOol)animated
{
[supervIEwDIDdisappear:animated];
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboarDWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboarDWillHIDeNotificationobject:nil];
}
- (voID)keyboarDWillShow:(NSNotification *)aNotification
{
// the keyboard is showing so resize the table's height
CGRect keyboardRect = [[[aNotificationuserInfo]objectForKey:UIKeyboardBoundsUserInfoKey]CGRectValue];
NSTimeInterval animationDuration = [[[aNotificationuserInfo]objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue];
CGRect frame =self.vIEw.frame;
frame.size.height -= keyboardRect.size.height;
[UIVIEwbeginAnimations:@"ResizeforKeyboard"context:nil];
[UIVIEwsetAnimationDuration:animationDuration];
self.vIEw.frame = frame;
[UIVIEwcommitAnimations];
}
- (voID)keyboarDWillHIDe:(NSNotification *)aNotification
{
// the keyboard is hIDing reset the table's height
CGRect keyboardRect = [[[aNotificationuserInfo]objectForKey:UIKeyboardBoundsUserInfoKey]CGRectValue];
NSTimeInterval animationDuration = [[[aNotificationuserInfo]objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue];
CGRect frame =self.vIEw.frame;
frame.size.height += keyboardRect.size.height;
[UIVIEwbeginAnimations:@"ResizeforKeyboard"context:nil];
[UIVIEwsetAnimationDuration:animationDuration];
self.vIEw.frame = frame;
[UIVIEwcommitAnimations];
}
9. UISegmentedControl
UISegmentedControl *segmentedControl = [[UISegmentedControlalloc]initWithItems: [NSArrayarrayWithObjects:
[UIImageimagenamed:@"segment_check.png"],[UIImageimagenamed:@"segment_search.png"],[UIImageimagenamed:@"segment_tools.png"],nil]];
yPlacement +=kTweenmargin +kLabelHeight;
frame = CGRectMake( kleftmargin,yPlacement,self.vIEw.bounds.size.wIDth - (kRightmargin * 2.0),kSegmentedControlHeight);
segmentedControl.frame = frame;
[segmentedControladdTarget:selfaction:@selector(segmentAction:)forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle =UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex =1;
NSArray *segmentTextContent = [NSArrayarrayWithObjects:@"Check",@"Search",@"Tools",nil];
segmentedControl = [[UISegmentedControlalloc]initWithItems:segmentTextContent];
yPlacement +=kTweenmargin +kLabelHeight;
frame = CGRectMake( kleftmargin,self.vIEw.bounds.size.wIDth - (kRightmargin * 2.0),kSegmentedControlHeight);
segmentedControl.frame = frame;
[segmentedControladdTarget:selfaction:@selector(segmentAction:)forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle =UISegmentedControlStylebordered;
segmentedControl.selectedSegmentIndex =1;
UISegmentedControl *segment = [[UISegmentedControlalloc]initWithFrame:];
[segment addTarget:selfaction:@selector(segmentedControlOnClick:)forControlEvents:UIControlEventValueChanged];
[segment insertSegmentWithImage:@"OK.png" atIndex:1animated:YES];
[segment insertSegmentWithImage:@"Cancle.png" atIndex:2animated:YES];
10.UIToolbar
// match each of the toolbar item's style match the selection in the "UIbarbuttonItemStyle" segmented control
UIbarbuttonItemStyle style =UIbarbuttonItemStylePlain; // UIbarbuttonItemStylebordered、UIbarbuttonItemStyleDone
// create the system-defined "OK or Done" button
UIbarbuttonItem *systemItem = [[UIbarbuttonItemalloc] initWithbarbuttonSystemItem:currentSystemItem target:selfaction:@selector(action:)];
systemItem.style = style;
// flex item used to separate the left groups items and right grouped items
UIbarbuttonItem *flexItem = [[UIbarbuttonItemalloc]initWithbarbuttonSystemItem:UIbarbuttonSystemItemFlexibleSpace target:nil action:nil];
// create a special tab bar item with a custom image and Title
UIbarbuttonItem *infoItem = [[UIbarbuttonItemalloc]initWithImage:[UIImageimagenamed:@"segment_tools.png"] style:style target:self
action:@selector(action:)];
// Set the accessibility label for an image bar item.
[infoItemsetAccessibilityLabel:NSLocalizedString(@"ToolsIcon",@"")];
// create a bordered style button with custom Title
UIbarbuttonItem *customItem = [[UIbarbuttonItemalloc]initWithTitle:@"Item" style:style// note you can use "UIbarbuttonItemStyleDone" to make it blue
target:self action:@selector(action:)];
NSArray *items = [NSArrayarrayWithObjects: systemItem,flexItem,customItem,infoItem,nil];
[self.toolbarsetItems:itemsanimated:NO];
11. UIAlertVIEw、UIActionSheet
// open a dialog with an OK and cancel button
UIActionSheet *actionSheet = [[UIActionSheetalloc]initWithTitle:@"UIActionSheet <Title>"
delegate:selfcancelbuttonTitle:@"Cancel"destructivebuttonTitle:@"OK"otherbuttonTitles:nil];
actionSheet.actionSheetStyle =UIActionSheetStyleDefault;
[actionSheetshowInVIEw:self.vIEw];// show from our table vIEw (pops up in the mIDdle of the table)
[actionSheet release];
// open a dialog with two custom buttons
UIActionSheet *actionSheet = [[UIActionSheetalloc]initWithTitle:@"UIActionSheet <Title>"
delegate:selfcancelbuttonTitle:nildestructivebuttonTitle:nil
otherbuttonTitles:@"button1",@"button2",nil];
actionSheet.actionSheetStyle =UIActionSheetStyleDefault;
actionSheet.destructivebuttonIndex =1;// make the second button red (destructive)
[actionSheetshowInVIEw:self.vIEw];// show from our table vIEw (pops up in the mIDdle of the table)
[actionSheet release];
// open a alert with an OK and cancel button
UIAlertVIEw *alert = [[UIAlertVIEwalloc]initWithTitle:@"UIAlertVIEw"message:@"<Alert message>"
delegate:selfcancelbuttonTitle:@"Cancel"otherbuttonTitles:@"OK",nil];
[alert show];
[alert release];
// open an alert with two custom buttons
UIAlertVIEw *alert = [[UIAlertVIEwalloc]initWithTitle:@"UIAlertVIEw"message:@"<Alert message>"
delegate:selfcancelbuttonTitle:@"Cancel"otherbuttonTitles:@"button1",@"button2",nil];
[alert show];
[alert release];
- (voID)actionSheet:(UIActionSheet *)actionSheet clickedbuttonAtIndex:(NSInteger)buttonIndex
{
// the user clicked one of the OK/Cancel buttons
}
- (voID)alertVIEw:(UIAlertVIEw *)actionSheet clickedbuttonAtIndex:(NSInteger)buttonIndex
{
}
12. 调用相机
UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];
picker.allowsEditing =YES;
picker.delegate = delegate;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalVIEwController:pickeranimated:YES];
[picker release];
13. 调用相册
UIImagePickerController* pickerImage = [[UIImagePickerControlleralloc]init];
pickerImage.delegate = delegate;
pickerImage.allowsEditing =YES;
[self presentModalVIEwController:pickerImageanimated:YES];
[pickerImage release];
#pragma mark - PickerController Delegate
- (voID)imagePickerController:(UIImagePickerController *)picker dIDFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
}
- (voID)imagePickerController:(UIImagePickerController *)picker dIDFinishPickingMediawithInfo:(NSDictionary *)info {
Nsstring *mediaType = [infoobjectForKey:UIImagePickerControllerMediaType];
if ([mediaTypeisEqualToString:@"public.image"]) {
CGSize size =CGSizeMake(120,140);
UIImage * i = [selfimageByScalingAndCropPingForSize:size image:[infoobjectForKey:@"UIImagePickerControllerEditedImage"]];
}
[picker dismissModalVIEwControllerAnimated:YES];
}
- (voID)imagePickerControllerDIDCancel:(UIImagePickerController *)picker {
[picker dismissModalVIEwControllerAnimated:YES];
}
14. 对图片内容进行base64编码
- (Nsstring *)base64:(UIImage *)image
{
NSData *basedata = UIImagePNGRepresentation(image);
NSData * data = [GTMBase64 encodeData:basedata];
Nsstring * base64String = [[[Nsstring alloc] initWithData:data enCoding:NSUTF8StringEnCoding] autorelease];
return base64String;
}
总结以上是内存溢出为你收集整理的cocoa FAQ之控件篇全部内容,希望文章能够帮你解决cocoa FAQ之控件篇所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)