限制字母、數字、符號等其他非中文字元的長度大家應該都知道咋整,但如果是中文輸入法,這就比較蛋疼了。舉個例子,限制長度為5,輸入“我愛寫程式碼”,當已經輸入“我愛寫”兩字,然後輸入“程式碼”時就會出現輸入後面幾位漢字被字母佔用位置而輸入不進去。解決辦法:
- (void)textViewDidChange:(UITextView *)textView
{
NSString *toBeString = textView.text;
// 獲取輸入法
NSString *lang = textView.textInputMode.primaryLanguage;
// 如果輸入法為中文
if ([lang isEqualToString:@"zh-Hans"]) {
// 這個range就是指輸入的拼音還沒有轉化成中文時的range
// 如果沒有,就表示已經轉成中文了
UITextRange *selectedRange = [textView markedTextRange];
if (!selectedRange && toBeString.length > 5) {
textView.text = [toBeString substringToIndex:5];
}
} else if (toBeString.length > 5) {
textView.text = [toBeString substringToIndex:5];
}
}
複製程式碼
#NSDate轉NSString 日期轉成字串。
+(NSString *)stringFromDate:(NSDate *)date
{
// //獲取系統當前時間
// NSDate *currentDate = [NSDate date];
//用於格式化NSDate物件
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//設定格式:zzz表示時區
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
//NSDate轉NSString
NSString *currentDateString = [dateFormatter stringFromDate:date];
//輸出currentDateString
NSLog(@"%@",currentDateString);
}
複製程式碼
NSDate物件包含兩個部分,日期(Date)和時間(Time)。格式化的時間字串主要也是針對日期和時間。NSDateFormatter是一個很常用的類,用於格式化NSDate物件,支援本地化的資訊。 NSDateFormatter常用格式有:
yyyy-MM-dd HH:mm:ss.sss
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd
MM dd yyy
複製程式碼
NSDateFormatter格式化引數如下:
G:公元時代,例如AD公元
yy:年的後2位
yyyy:完整年
MM:月,顯示為1-12
MMM:月,顯示為英文月份簡寫,如 Jan
MMMM:月,顯示為英文月份全稱,如 Janualy
dd:日,2位數表示,如02
d:日,1-2位顯示,如 2、11
EEE:簡寫星期幾,如Sun
EEEE:全蠍星期幾,如Sunday
aa:上下午,AM/PM
H:時,24小時制,0-23
K:時,12小時制,0-11
m:分,1-2位
mm:分,2位
s:秒,1-2位
ss:秒,2位
S:毫秒
複製程式碼
#NSString轉NSDate 字串轉日期
+ (NSDate *)dateFromString:(NSString *)string
{
//設定轉換格式
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//NSString轉NSDate
NSDate *date = [formatter dateFromString:dateString];
return date;
}
複製程式碼
參考網站:http://www.superqq.com/blog/2015/06/26/nsdatehe-nsstringxiang-hu-zhuan-huan/ 日曆和時間的計算可參考:http://www.jianshu.com/p/692bb0a7269c
#iOS 監聽鍵盤的顯示與隱藏,並實現輸入框緊緊跟隨在鍵盤的上方
// 在-viewDidLoad方法中註冊通知,監聽鍵盤的彈出和收回
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
複製程式碼
###pragma mark 鍵盤顯示的監聽方法
-(void) keyboardWillShow:(NSNotification *) note
{
// 獲取鍵盤的位置和大小
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// Need to translate the bounds to account for rotation.
keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
// 獲取輸入框的位置和大小
CGRect containerFrame = _inputView.frame;
// 計算出輸入框的y座標
containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height);
// 動畫改變位置
[UIView animateWithDuration:[duration doubleValue] animations:^{
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
// 更改輸入框的位置
_inputView.frame = containerFrame;
}];
}
複製程式碼
###pragma mark 鍵盤隱藏的監聽方法
-(void) keyboardWillHide:(NSNotification *) note
{
NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// 獲取輸入框的位置和大小
CGRect containerFrame = _inputView.frame;
containerFrame.origin.y = self.view.bounds.size.height - containerFrame.size.height;
// 動畫改變位置
[UIView animateWithDuration:[duration doubleValue] animations:^{
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
// 更改輸入框的位置
_inputView.frame = containerFrame;
}];
}
複製程式碼
_inputView便是存放輸入框和View,就是跟隨鍵盤上下移動的UIView ///////