Gowhich

Durban's Blog

出现问题的代码如下:

1
2
3
4
5
6
7
8
9
//选择按钮
UIButton *performerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[performerButton setFrame:CGRectMake(tip.frame.size.width+10.0, y+11.0, 35.0, 25.0)];
[performerButton addTarget:self
action:@selector(setTeleplayType:)
forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:performerButton];
[performerButton setTitle:@"演员" forState:UIControlStateNormal];
[performerButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

结果是看不到你添加的标题。

问题的关键是如何解决的,呵呵,蠢死我了,其实就是自己设计的button的宽度太小了,没有将字体显示出来.

结果的代码如下修改就好:

1
2
3
4
5
6
7
8
9
//选择按钮
UIButton *performerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[performerButton setFrame:CGRectMake(tip.frame.size.width+10.0, y+11.0, 45.0, 25.0)];
[performerButton addTarget:self
action:@selector(setTeleplayType:)
forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:performerButton];
[performerButton setTitle:@"演员" forState:UIControlStateNormal];
[performerButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

由于自己也在学习ios开发,也经常的会关注developer.app.com,因此今天有幸看到这篇文章,自己英语也不是很好,综合google和海词,大概了解了一下标题的意思,(https://developer.apple.com/library/ios/#qa/qa1763/_index.html#//apple_ref/doc/uid/DTS40012165)这个是文章的地址,这里算是做个笔记,方便自己下次看看。这篇文章整体就是说代表一个团队分发一个app,应该是用于这个团队的测试使用吧。(也许有错,还希望大牛看到后 指点一下)。

那么接下来的结局方案就是如下了:

  1. Add the build engineer to the development team with the role of Admin through the Member Center. For more information on defining team roles, see Managing Your Team. The Admin role is required to manage the team distribution certificate.

  2. If the distribution build will be submitted to the App Store, add the build engineer to the team users on iTunes Connect with the role of Technical User. This allows the build engineer to log into iTunes Connect with their own credentials while submitting the app. Skip this step if the build is being used for Ad Hoc beta testing. For more info about the Technical User role in iTunes Connect, see Managing Users in the iTunes Connect Developer Guide.

  3. The build engineer should then…

  • Create a new distribution certificate according to Creating Your Certificates. If the team distribution certificate already exists, it must be revoked and recreated by the build engineer according to Re-Creating Certificates and Updating Related Provisioning Profiles.
  • Create a distribution profile using the steps in Creating Store Provisioning Profiles.
  • Download and install the distribution provisioning profile by dragging it onto the Xcode or iTunes icons on the dock.
  • Define a Bundle Identifier in Xcode that is compatible with the App ID for the app using the steps in Setting the Bundle ID.
  • Assign the distribution provisioning profile to the ‘Release’ Code Signing Identity with the Xcode project Target level Build Settings.
  • Depending on your intended distribution method, follow the steps in Submitting Your App or Beta Testing your iOS App respectively.

我就直接复制了一下,其实步骤很简单的,基本上能够看的明白。

只不过这里提到了几点Importment:

This Q&A is exclusively for iOS App Store apps. The process within this document should not be followed for the Enterprise Developer Program. Please consult Apple DTS with any questions before leveraging this document, or if you are unsure of your developer program type.

这条就是说这个需求不适合企业开发者项目,显然不是企业开发者,就是个人的喽,呵呵。

另外一点就是

A single email address has access to only one iTunes Connect account, unlike the Member Center, which supports multiple teams per Apple ID. Therefore the build engineer must supply a new unique email address that is not already associated to another iTunes Connect account.

一个email地址只允许链接一个iTunes账户,作为一个bulid enginner 必须有一个email,这个email没有链接多个iTunes账户。好了就这么多了。有不懂的大家一起探讨

NSDateFormatter自定义日期/时间格式 ,简单的举一个例子,看下面的代码就可以了

1
2
3
4
5
6
7
NSDate *dateValue = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
dateValue = ((UIDatePicker *)sender).date;

self.teleplayDate.text = [dateFormatter stringFromDate:dateValue];//[NSString stringWithFormat:@"%@",dateValue];
self.wantToSaveTeleplayDate = self.teleplayDate.text;

下面附上几个格式:

1
2
3
4
5
6
yyyy:MM:dd G 'at' HH:mm:ss zzz      1996.07.10 AD at 15:08:56 PDT  
EEE, MMM d, "yy Wed,july 10, '99
h:mm a 12:08 PM
hh 'o"clock' a,zzzz 12 o'clock PM, Pacific Daylight Time
K:mm a, z 0:00 PM, PST
yyyyy,MMMM.dd GGG hh:mm aaa 01996.july.10 AD 12:08 PM

下面是得到当前的年,月,日,时,分,秒。

1
2
3
4
5
6
7
8
9
NSCalendar *cal = [NSCalendar currentCalendar];  
unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dd = [cal components:unitFlags fromDate:date];
int y = [dd year];
int m = [dd month];
int d = [dd day];
int h = [dd hour];
int m = [dd minute];
int s = [dd second];

当然不止这些的,有兴趣的可以去参考一下:http://unicode.org/reports/tr35/tr35-6.html#Date\_Format\_Patterns

自定义的UIActionSheet ,网上找了好久,都没有完整的解决办法,于是自己 经过查找资料,然后根据自己的项目需要做了一些改动,得到了一个完整版的,自定义的UIActionSheet,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
-(void) setUpDatePicker
{
//点击显示时间
self.actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];

UISegmentedControl *cancelButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"取消"]];
UISegmentedControl *confirmButton =[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"确定"]];
[self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
// Add the picker
self.datePicker = [[UIDatePicker alloc] init];
self.datePicker.datePickerMode = UIDatePickerModeDate;
[self.datePicker addTarget:self
action:@selector(dateChanged:)
forControlEvents:UIControlEventValueChanged];
[self.actionSheet addSubview:self.datePicker];
[self.actionSheet showInView:self.view];
[self.actionSheet setBounds:CGRectMake(0,0,320, 500)];

CGRect pickerRect;
pickerRect = self.datePicker.bounds;
pickerRect.origin.y = -50;
self.datePicker.bounds = pickerRect;
cancelButton.momentary = YES;
cancelButton.frame = CGRectMake(10.0f, 7.0f, 65.0f, 32.0f);
cancelButton.segmentedControlStyle = UISegmentedControlStyleBar;
[cancelButton addTarget:self action:@selector(DatePickerDoneClick:) forControlEvents:UIControlEventValueChanged];
[self.actionSheet addSubview:cancelButton];

cancelButton.tag = 1;
confirmButton.momentary = YES;
confirmButton.frame = CGRectMake(245.0f, 7.0f, 65.0f, 32.0f);
confirmButton.segmentedControlStyle = UISegmentedControlStyleBar;
[confirmButton addTarget:self action:@selector(DatePickerDoneClick:) forControlEvents:UIControlEventValueChanged];
[self.actionSheet addSubview:confirmButton];

confirmButton.tag = 2;
[self.actionSheet showInView:self.view];
[self.actionSheet setBounds:CGRectMake(0,0, 320, 500)];

}

-(void) DatePickerDoneClick:(id) sender
{
UIButton *button = (UIButton *)sender;
if(button.tag == 1)
{
[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

if(button.tag == 2)
{
[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
}

-(void) dateChanged:(id)sender
{
NSDate *dateValue = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
dateValue = ((UIDatePicker *)sender).date;

self.teleplayDate.text = [dateFormatter stringFromDate:dateValue];//[NSString stringWithFormat:@"%@",dateValue];
}

这里简单的解释一下:

  • -(void) setUpDatePicker方法用于调用UIDatePicker

  • -(void) DatePickerDoneClick:(id) sender方法用于实现隐藏UIdatePicker

  • -(void) dateChanged:(id)sender方法实现获取日期结果值的方法。

如果没有实现效果,别忘记加上协议,这个是比较容易忘记的

IOS关于NSString追加字符串的问题,网上也有不少的做法,比较多的做法是:

1
2
3
4
5
6
NSMutableString *str=[[NSMutableStringalloc] initWithString:@"dd"];
[str stringByAppendingString:@"eee" ]; //问题行
NSLog(str);
//一开始的时候怎样修改都追加不上,类型也换了
//应该是:把追加后的值回传给要追加的原对象
str=[str stringByAppendingString:@"eee" ]; //正确

但是使用上面的方法,我不知道你们有没有遇到有类型不对的提示,总之我这里是有的,于是我将上面这样改了一下:

1
2
3
NSString *str=[[NSMutableStringalloc] initWithString:@"dd"];
[str stringByAppendingString:@"eee" ]; //问题行
NSLog(str);

一开始的时候怎样修改都追加不上,类型也换了
应该是:把追加后的值回传给要追加的原对象

1
str=[str stringByAppendingString:@"eee" ]; //正确

警告消失了。

点击屏幕任意空白处,键盘消失的方法:

在这个方法里面实现就好了:

1
2
3
4
5
6
7
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.teleplayDescription resignFirstResponder];
[self.teleplayTitle resignFirstResponder];
[self.teleplayContactPeople resignFirstResponder];
[self.teleplayContactPeoplePhone resignFirstResponder];
}

在viewdidload的时候,把每个TextField设好tag。之后就可以根据最下面的UITextField的内容来判断键盘的弹出和关闭了

实例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
- (void)textFieldDidBeginEditing:(UITextField *)textField 

{ //当点触textField内部,开始编辑都会调用这个方法。textField将成为first responder

if (textField.tag == 2) {

NSTimeInterval animationDuration = 0.30f;

CGRect frame = self.view.frame;

frame.origin.y -=216;

frame.size.height +=216;

self.view.frame = frame;

[UIView beginAnimations:@"ResizeView"context:nil];

[UIView setAnimationDuration:animationDuration];

self.view.frame = frame;

[UIView commitAnimations];

}
}



- (BOOL)textFieldShouldReturn:(UITextField *)textField

{//当用户按下ruturn,把焦点从textField移开那么键盘就会消失了

// textField

if (textField.tag == 2) {

NSTimeInterval animationDuration = 0.30f;

CGRect frame = self.view.frame;

frame.origin.y +=216;

frame.size. height -=216;

self.view.frame = frame;

//self.view移回原位置

[UIView beginAnimations:@"ResizeView"context:nil];

[UIView setAnimationDuration:animationDuration];

self.view.frame = frame;

[UIView commitAnimations];

}

[textField resignFirstResponder];

returnYES;
}

  • 初始化textfield并设置位置及大小
    UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];

  • 设置边框样式,只有设置了才会显示边框样式

1
2
3
4
5
6
7
8
9
10
11
text.borderStyle = UITextBorderStyleRoundedRect;  
typedef enum {
UITextBorderStyleNone,

UITextBorderStyleLine,

UITextBorderStyleBezel,

UITextBorderStyleRoundedRect

} UITextBorderStyle;
  • 设置输入框的背景颜色,此时设置为白色 如果使用了自定义的背景图片边框会被忽略掉
    text.backgroundColor = [UIColor whiteColor];

  • 设置背景
    text.background = [UIImage imageNamed:@"dd.png"];

  • 设置背景
    text.disabledBackground = [UIImage imageNamed:@"cc.png"];

  • 当输入框没有内容时,水印提示 提示内容为password
    text.placeholder = @"password";

  • 设置输入框内容的字体样式和大小
    text.font = [UIFont fontWithName:@"Arial" size:20.0f];

  • 设置字体颜色
    text.textColor = [UIColor redColor];

  • 输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容

1
2
3
4
5
6
7
8
9
10
11
12
text.clearButtonMode = UITextFieldViewModeAlways;  
typedef enum {

UITextFieldViewModeNever, // 重不出现

UITextFieldViewModeWhileEditing, //编辑时出现

UITextFieldViewModeUnlessEditing, // 除了编辑外都出现

UITextFieldViewModeAlways // 一直出现

} UITextFieldViewMode;
  • 输入框中一开始就有的文字
    text.text = @"一开始就在输入框的文字";

  • 每输入一个字符就变成点 用语密码输入
    text.secureTextEntry = YES;

  • 是否纠错

1
2
3
4
5
6
7
8
9
10
text.autocorrectionType = UITextAutocorrectionTypeNo;  
typedef enum {

UITextAutocorrectionTypeDefault, // 默认

UITextAutocorrectionTypeNo, // 不自动纠错

UITextAutocorrectionTypeYes, // 自动纠错

} UITextAutocorrectionType;
  • 再次编辑就清空
    text.clearsOnBeginEditing = YES;

  • 内容对齐方式
    text.textAlignment = UITextAlignmentLeft;

  • 内容的垂直对齐方式 UITextField继承自UIControl, 此类中有一个属性contentVerticalAlignment
    text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

  • 设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动
    textFied.adjustsFontSizeToFitWidth = YES;

  • 设置自动缩小显示的最小字体大小
    text.minimumFontSize = 20;

  • 设置键盘的样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
text.keyboardType = UIKeyboardTypeNumberPad;  
typedef enum {

UIKeyboardTypeDefault, // 默认键盘,支持所有字符

UIKeyboardTypeASCIICapable, // 支持ASCII的默认键盘

UIKeyboardTypeNumbersAndPunctuation, // 标准电话键盘,支持+*#字符

UIKeyboardTypeURL, // URL键盘,支持.com按钮 只支持URL字符

UIKeyboardTypeNumberPad, // 数字键盘

UIKeyboardTypePhonePad, // 电话键盘

UIKeyboardTypeNamePhonePad, // 电话键盘,也支持输入人名

UIKeyboardTypeEmailAddress, // 用于输入电子 邮件地址的键盘

UIKeyboardTypeDecimalPad, // 数字键盘 有数字和小数点

UIKeyboardTypeTwitter, // 优化的键盘,方便输入@、#字符

UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,

} UIKeyboardType;
  • 首字母是否大写
1
2
3
4
5
6
7
8
9
10
11
12
text.autocapitalizationType = UITextAutocapitalizationTypeNone;  
typedef enum {

UITextAutocapitalizationTypeNone, // 不自动大写

UITextAutocapitalizationTypeWords, // 单词首字母大写

UITextAutocapitalizationTypeSentences, // 句子的首字母大写

UITextAutocapitalizationTypeAllCharacters, // 所有字母都大写

} UITextAutocapitalizationType;
  • return键变成什么键
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
text.returnKeyType =UIReturnKeyDone;  
typedef enum {

UIReturnKeyDefault, // 默认 灰色按钮,标有Return

UIReturnKeyGo, // 标有Go的蓝色按钮

UIReturnKeyGoogle, // 标有Google的蓝色按钮,用语搜索

UIReturnKeyJoin, // 标有Join的蓝色按钮

UIReturnKeyNext, // 标有Next的蓝色按钮

UIReturnKeyRoute, // 标有Route的蓝色按钮

UIReturnKeySearch, // 标有Search的蓝色按钮

UIReturnKeySend, // 标有Send的蓝色按钮

UIReturnKeyYahoo, // 标有Yahoo的蓝色按钮

UIReturnKeyYahoo, // 标有Yahoo的蓝色按钮

UIReturnKeyEmergencyCall, // 紧急呼叫按钮

} UIReturnKeyType;
  • 键盘外观
1
2
3
4
5
6
7
8
textView.keyboardAppearance=UIKeyboardAppearanceDefault
typedef enum {

UIKeyboardAppearanceDefault// 默认外观,浅灰色

UIKeyboardAppearanceAlert// 深灰 石墨色

} UIReturnKeyType;
  • 设置代理 用于实现协议
    text.delegate = self;

  • 把textfield加到视图中
    [self.window addSubview:text];

  • 最右侧加图片是以下代码 左侧类似

1
2
3
4
5
6
7
8
9
10
11
12
13
14
UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];  
text.rightView=image;
text.rightViewMode = UITextFieldViewModeAlways;
typedef enum {

UITextFieldViewModeNever,

UITextFieldViewModeWhileEditing,

UITextFieldViewModeUnlessEditing,

UITextFieldViewModeAlways

} UITextFieldViewMode;
  • 按return键键盘往下收 becomeFirstResponder
    类要采用UITextFieldDelegate协议 text.delegate = self; 声明text的代理是我,我会去实现把键盘往下收的方法 这个方法在UITextFieldDelegate里所以我们要采用UITextFieldDelegate这个协议
1
2
3
4
5
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[text resignFirstResponder]; //主要是[receiver resignFirstResponder]在哪调用就能把receiver对应的键盘往下收

return YES;
}

重写绘制行为

除了UITextField对象的风格选项,你还可以定制化UITextField对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围。以下方法都可以重写。

  • textRectForBounds: // 重写来重置文字区域

  • drawTextInRect: // 改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.

  • placeholderRectForBounds: // 重写来重置占位符区域

  • drawPlaceholderInRect: // 重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.

  • borderRectForBounds: // 重写来重置边缘区域

  • editingRectForBounds: // 重写来重置编辑区域

  • clearButtonRectForBounds: // 重写来重置clearButton位置,改变size可能导致button的图片失真

  • leftViewRectForBounds:

  • rightViewRectForBounds:

委托方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ 
//返回一个BOOL值,指定是否循序文本字段开始编辑
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//开始编辑时触发,文本字段将成为first responder
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
//返回BOOL值,指定是否允许文本字段结束编辑,当编辑结束,文本字段会让出first responder
//要想在用户结束编辑时阻止文本字段消失,可以返回NO
//这对一些文本字段必须始终保持活跃状态的程序很有用,比如即时消息
return NO;
}
- (BOOL)textField:(UITextField\*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

//当用户使用自动更正功能,把输入的文字修改为推荐的文字时,就会调用这个方法。

//这对于想要加入撤销选项的应用程序特别有用

//可以跟踪字段内所做的最后一次修改,也可以对所有编辑做日志记录,用作审计用途。

//要防止文字被改变可以返回NO

//这个方法的参数中有一个NSRange对象,指明了被改变文字的位置,建议修改的文本也在其中

return YES;

}
- (BOOL)textFieldShouldClear:(UITextField \*)textField{
//返回一个BOOL值指明是否允许根据用户请求清除内容
//可以设置在特定条件下才允许清除内容
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField \*)textField{
//返回一个BOOL值,指明是否允许在按下回车键时结束编辑
//如果允许要调用resignFirstResponder 方法,这回导致结束编辑,而键盘会被收起[textField resignFirstResponder];
//查一下resign这个单词的意思就明白这个方法了
return YES;
}

通知

UITextField派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用。除了UIControl类的标准事件,你还可以使用下列UITextField类特有的事件

  • UITextFieldTextDidBeginEditingNotification
  • UITextFieldTextDidChangeNotification
  • UITextFieldTextDidEndEditingNotification

当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。
因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知

  • UIKeyboardWillShowNotification //键盘显示之前发送
  • UIKeyboardDidShowNotification //键盘显示之后发送
  • UIKeyboardWillHideNotification //键盘隐藏之前发送
  • UIKeyboardDidHideNotification //键盘隐藏之后发送

限制只能输入特定的字符

1
2
3
4
5
6
7
8
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *cs;
cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
BOOL canChange = [string isEqualToString:filtered];
return canChange;
}

上面那个NUMBERS是一个宏,可以在文件顶部定义:
#define NUMBERS @”0123456789\n” (这个代表可以输入数字和换行,请注意这个\n,如果不写这个,Done按键将不会触发,如果用在SearchBar中,将会不触发Search事件,因为你自己限制不让输入\n,好惨,我在项目中才发现的。)
所以,如果你要限制输入英文和数字的话,就可以把这个定义为:
#define kAlphaNum @”ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789″
当然,你还可以在以上方法return之前,做一提示的,比如提示用户只能输入数字之类的。如果你觉得有需要的话。
限制只能输入一定长度的字符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
//string就是此时输入的那个字符 textField就是此时正在输入的那个输入框 返回YES就是可以改变输入框的值 NO相反
if ([string isEqualToString:@"\n"]) //按会车可以改变
{
return YES;
}
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
if (self.myTextField == textField) //判断是否时我们想要限定的那个输入框
{

if ([toBeString length] > 20) { //如果输入框内容大于20则弹出警告

textField.text = [toBeString substringToIndex:20];

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil message:@"超过最大字数不能输入了" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] autorelease];

[alert show];

return NO;

}

}
return YES;
}

我在用UITextView的时候,默认是不能设置placeholder的,于是网上找了一些资料,最后找到了一个别人写的自定义类,拿来用用,顺便分享一下。

CPTextViewPlaceholder.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// CPTextViewPlaceholder.m
// Cassius Pacheco
//
// Created by Cassius Pacheco on 30/01/13.
// Copyright (c) 2013 Cassius Pacheco. All rights reserved.
//

#import "CPTextViewPlaceholder.h"

@interface CPTextViewPlaceholder()

@property (nonatomic) UITextAutocorrectionType originalCorrection;
@property (nonatomic, strong) UIColor *placeholderColor;
@property (nonatomic, strong) UIColor *originalTextColor;
@property (nonatomic, getter = isUsingPlaceholder) BOOL usingPlaceholder;
@property (nonatomic, getter = isSettingPlaceholder) BOOL settingPlaceholder;

@end

@implementation CPTextViewPlaceholder

#pragma mark -
#pragma mark Life Cycle method

- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {

self.placeholderColor = [UIColor lightGrayColor];
self.originalCorrection = self.autocorrectionType;
self.originalTextColor = super.textColor;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didBeginEditing) name:UITextViewTextDidBeginEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEndEditing) name:UITextViewTextDidEndEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self];
}

return self;
}

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidBeginEditingNotification object:self];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidEndEditingNotification object:self];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:self];
}

- (void)layoutSubviews
{
[super layoutSubviews];

//Fixes iOS 5.x cursor when becomeFirstResponder
if ([UIDevice currentDevice].systemVersion.floatValue < 6.000000) {
if (self.isUsingPlaceholder && self.isFirstResponder) {
self.text = @"";
}
}

}

#pragma mark -
#pragma mark Notifications

- (void)didBeginEditing
{
if (self.isUsingPlaceholder) {
[self sendCursorToBeginning];
}
}

- (void)didEndEditing
{
if (self.text.length == 0) {
[self setupPlaceholder];
}
}

- (void)textDidChange:(NSNotification *)notification
{
//self.text received the placeholder text by CPTex tViewPlaceholder
if (self.isSettingPlaceholder) {
return;
}

if (self.text.length == 0) {
[self setupPlaceholder];
return;
}

if (self.isUsingPlaceholder) {
self.usingPlaceholder = NO;
NSRange range = [self.text rangeOfString:self.placeholder options:NSLiteralSearch];

if (range.location != NSNotFound) {
NSString *newText = [self.text stringByReplacingCharactersInRange:range withString:@""];
super.textColor = self.originalTextColor;
super.autocorrectionType = self.originalCorrection;

//User pasted a text equals to placeholder or setText was called
if ([newText isEqualToString:self.placeholder]) {
[self sendCursorToEnd];
//this is necessary for iOS 5.x
} else if (newText.length == 0) {
[self setupPlaceholder];
return;
}

self.text = newText;
}
}
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (self.isUsingPlaceholder && action != @selector(paste:)) {
return NO;
}

return [super canPerformAction:action withSender:sender];
}

#pragma mark - Getters and Setters

- (void)setAutocorrectionType:(UITextAutocorrectionType)autocorrectionType
{
[super setAutocorrectionType:autocorrectionType];
self.originalCorrection = autocorrectionType;
}

- (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = placeholder;

if (self.isUsingPlaceholder || self.text.length == 0) {
[self setupPlaceholder];
}
}

- (void)setTextColor:(UIColor *)textColor
{
[super setTextColor:textColor];
self.originalTextColor = textColor;
}

- (void)setSelectedRange:(NSRange)selectedRange
{
if (self.isUsingPlaceholder) {
[self sendCursorToBeginning];
} else {
[super setSelectedRange:selectedRange];
}
}

- (void)setSelectedTextRange:(UITextRange *)selectedTextRange
{
if (self.isUsingPlaceholder) {
[self sendCursorToBeginning];
} else {
[super setSelectedTextRange:selectedTextRange];
}
}

#pragma mark -
#pragma mark Utilities methods

- (void)setupPlaceholder
{
super.autocorrectionType = UITextAutocorrectionTypeNo;
self.usingPlaceholder = YES;
self.settingPlaceholder = YES;
self.text = self.placeholder;
self.settingPlaceholder = NO;
super.textColor = self.placeholderColor;
[self sendCursorToBeginning];
}

- (void)sendCursorToBeginning
{
[self performSelector:@selector(cursorToBeginning) withObject:nil afterDelay:0.01];
}

- (void)cursorToBeginning
{
super.selectedRange = NSMakeRange(0, 0);
}

- (void)sendCursorToEnd
{
[self performSelector:@selector(cursorToEnd) withObject:nil afterDelay:0.01];
}

- (void)cursorToEnd
{
super.selectedRange = NSMakeRange(self.text.length, 0);
}

@end
CPTextViewPlaceholder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//
// CPTextViewPlaceholder.h
// Cassius Pacheco
//
// Created by Cassius Pacheco on 30/01/13.
// Copyright (c) 2013 Cassius Pacheco. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CPTextViewPlaceholder : UITextView

@property (nonatomic, strong) NSString *placeholder;

@end

创建一个新的repository:

先在github上创建并写好相关名字,描述。(github的创建,可以参考这里https://help.github.com/articles/create-a-repo)

如果我们创建了hello-world,那么下面的操作如下:

1
2
3
4
5
6
$cd ~/hello-world  //到hello-world目录
$git init //初始化
$git add .//把所有文件加入到索引(不想把所有文件加入,可以用gitignore或add 具体文件)
$git commit //提交到本地仓库,然后会填写更新日志( -m “更新日志”也可)
$git remote add origin xx@xx:zhangda89/hello-world.git //增加到remote
$git push origin master //push到github上

更新项目(新加了文件):

1
2
3
4
$cd ~/hello-world
$git add . //这样可以自动判断新加了哪些文件,或者手动加入文件名字
$git commit //提交到本地仓库
$git push origin master //不是新创建的,不用再add 到remote上了

更新项目(没新加文件,只有删除或者修改文件):

1
2
3
$cd ~/hello-world
$git commit -a //记录删除或修改了哪些文件
$git push origin master //提交到github

忽略一些文件,比如*.o等:

1
2
$cd ~/hello-world
$vim .gitignore //把文件类型加入到.gitignore中,保存

然后就可以git add . 能自动过滤这种文件

clone代码到本地:

1
2
3
$git clone xx@xx:zhangda89/hello-world.git //假如本地已经存在了代码,而仓库里有更新,把更改的合并到本地的项目:
$git fetch origin //获取远程更新
$git merge origin/master //把更新的内容合并到本地分支

撤销

1
$git reset

删除

1
$git rm  * // 不是用rm

常见错误

错误提示:fatal: remote origin already exists.

解决办法: $git remote rm origin

然后在执行:$git remote add origin [email protected]:zhangda89/hello-world.git 就不会报错误了

  • git push origin master

错误提示:error:failed to push som refs to

解决办法: $git pull origin master //先把远程服务器github上面的文件拉先来,再push 上去。

0%