cocoa FAQ之控件篇
来源:互联网 发布:奢侈品品牌 知乎 编辑:程序博客网 时间:2024/06/03 19:07
注:代码摘自苹果官方"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,12.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,yPlacement,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:sizeimage:[infoobjectForKey:@"UIImagePickerControllerEditedImage"]];
}
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
14. 对图片内容进行base64编码
- (NSString *)base64:(UIImage *)image
{
NSData *basedata =UIImagePNGRepresentation(image);
NSData * data = [GTMBase64encodeData:basedata];
NSString * base64String = [[[NSStringalloc] initWithData:data encoding:NSUTF8StringEncoding]autorelease];
return base64String;
}
- cocoa FAQ之控件篇
- 手把手教你编写cocoa Calendar控件之MonthView
- [Cocoa]深入浅出 Cocoa 之消息
- [Cocoa]深入浅出 Cocoa 之 Framework
- [Cocoa]深入浅出 Cocoa 之 Plugin
- [Cocoa]深入浅出 Cocoa 之 Framework
- [Cocoa]深入浅出 Cocoa 之消息
- [Cocoa]深入浅出 Cocoa 之消息
- [Cocoa]深入浅出Cocoa之消息
- [Cocoa]深入浅出Cocoa之消息
- [Cocoa]深入浅出 Cocoa 之消息
- [Cocoa]深入浅出Cocoa之消息
- [Cocoa]深入浅出Cocoa之消息
- [Cocoa]深入浅出 Cocoa 之消息
- [Cocoa]深入浅出 Cocoa 之消息
- DotNetTextBox控件常见问题FAQ
- 百度之站长FAQ - 收录篇【一】
- 百度之站长FAQ - 收录篇【二】
- 四、CentOS 命令汇集
- 高性能web不错的专题
- ubuntu安装eclipse
- 删除链表节点
- 单链表转置
- cocoa FAQ之控件篇
- 编程技术面试的五大要点
- IOS开发常用的开源类库和一些示例
- Air Native Extension Example Hello World
- 深入jvm专题
- AJAX POST和GET区别
- 请原谅篇
- JAVA异常处理相关面试题
- CHandleImage 自己写的用GDI+处理图像的一个类