//記得設(shè)置代理
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITextView* textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 20, 300, 330)];
textView.backgroundColor = [UIColor grayColor];
//文本
textView.text = @"aaweqtrehgtbwsagas 123456 撒旦法師打發(fā)四的發(fā)生的 阿斯頓發(fā)送到發(fā)送到發(fā)阿斯頓發(fā)生阿斯蒂芬 撒旦法阿斯蒂芬";
//字體
textView.font = [UIFont boldSystemFontOfSize:20.0];
//對齊
textView.textAlignment = NSTextAlignmentCenter;
//字體顏色
textView.textColor = [UIColor redColor];
//允許編輯
textView.editable = YES;
//用戶交互 ///////若想有滾動(dòng)條 不能交互 上為No,下為Yes
textView.userInteractionEnabled = YES; ///
//自定義鍵盤
//textView.inputView = view;//自定義輸入?yún)^(qū)域
//textView.inputAccessoryView = view;//鍵盤上加view
textView.delegate = self;
[self.view addSubview:textView];
textView.scrollEnabled = YES;//滑動(dòng)
textView.returnKeyType = UIReturnKeyDone;//返回鍵類型
textView.keyboardType = UIKeyboardTypeDefault;//鍵盤類型
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自適應(yīng)
textView.dataDetectorTypes = UIDataDetectorTypeAll;//數(shù)據(jù)類型連接模式
textView.autocorrectionType = UITextAutocorrectionTypeNo;//自動(dòng)糾錯(cuò)方式
textView.autocapitalizationType = UITextAutocapitalizationTypeNone;//自動(dòng)大寫方式
//禁止文字居中或下移64,因?yàn)閍vigationController下scrollView自動(dòng)適應(yīng)屏幕,而UITextView繼承自UIScrollView
if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
//[self testMPItem];
}
//////////事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//判斷類型,如果不是UITextView類型,收起鍵盤
for (UIView* view in self.view.subviews) {
if ([view isKindOfClass:[UITextView class]]) {
UITextView* tv = (UITextView*)view;
[tv resignFirstResponder];
}
}
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView{
NSLog(@"開始編輯");
}
- (void)textViewDidEndEditing:(UITextView *)textView{
NSLog(@"結(jié)束編輯");
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
//控制文本輸入內(nèi)容
if (range.location>=100){
//控制輸入文本的長度
return NO;
}
if ([text isEqualToString:@"\n"]){
//禁止輸入換行
return NO;
}
return YES;
}
- (void)textViewDidChange:(UITextView *)textView{
NSLog(@"已經(jīng)修改");
//自適應(yīng)文本高度
//計(jì)算文本的高度
CGSize constraintSize;
constraintSize.width = textView.frame.size.width-16;
constraintSize.height = MAXFLOAT;
CGSize sizeFrame =[textView.text sizeWithFont:textView.font
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
//重新調(diào)整textView的高度
textView.frame = CGRectMake(textView.frame.origin.x,textView.frame.origin.y,textView.frame.size.width,sizeFrame.height+5);
}
- (void)textViewDidChangeSelection:(UITextView *)textView{
NSLog(@"textViewDidChangeSelection");
}
iOS-UITextView 文本輸入框
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 此中所述兩種UI控件,皆為文本輸入控件,即可接收用戶輸入的UI控件。 >>>UITextField是作為文字輸入控...
- UITextView 實(shí)現(xiàn)占位文本的方式有很多種,網(wǎng)上一搜一大把,,這里只介紹 最簡單 的一種,如標(biāo)題所述:用 R...