UITextView的一些使用技巧
1.在指定位置插入字符串:
NSMutableString *TextViewStr=[[NSMutableString alloc] initWithString:TextView.text]; [TextViewStr insertString:@"your strings" atIndex:TextView.selectedRange.location]; TextView.scrollEnabled=NO; TextView.text=theTvStr; theTV.scrollEnabled=YES;
2.獲得行數(包括換行符也會計算在內):
CGSize size = [[self.TextView text] sizeWithFont:[self.TextView font]]; // 2. 取出文字的高度 int length = size.height; //3. 計算行數 int colomNumber = TextView.contentSize.height/length;
3.檢測換行符:
- (BOOL)textView: (UITextView *)textview shouldChangeTextInRange: (NSRange)range replacementText: (NSString *)text { if ([text isEqualToString:@"\n"]) { NSLog(@"it is a row !!"); //... } return YES; }<pre name="code" class="objc"> self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease]; //初始化大小并自動釋放 self.textView.textColor = [UIColor blackColor];//設置textview里面的字體顏色 self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];//設置字體名字和字體大小 self.textView.delegate = self;//設置它的委托方法 self.textView.backgroundColor = [UIColor whiteColor];//設置它的背景顏色 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;//鍵盤類型 self.textView.scrollEnabled = YES;//是否可以拖動 self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自適應高度 [self.view addSubview: self.textView];//加入到整個頁面中
注釋:
文本字段實現了 UITextInputTrait協議,其提供了7個屬性來定義字段處理文本輸入的方式:autocapitalizationType、 autocorrectionType、enablesReturnKeyAutomatically、keyboardAppearance、 keyboardType、returnKeyType、secureTextEntry。其它,當文本字段為空時,placeholder文本以淺灰色顯示,提供一個用戶提示。通過設置clearButtonMode可以指定是否以及何時顯示清除按鈕。
如果你的textview里不用回車鍵,可以把回車鍵當做退出鍵盤的響應鍵。
#pragma mark - UITextView Delegate Methods -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if ([text isEqualToString:@"\n"]) { [textView resignFirstResponder]; return NO; } return YES; }
本文由用戶 wdfd 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!