有時候我們不是從服務器上直接加載一個網頁,而是需要加載本地的html文件,具體實現的代碼如下:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//創建從Bundle中來自HTML文件的URL
NSURL *html = [[NSBundle mainBundle] URLForResource:@"right_obligations" withExtension:@"html"];
// 用HTML創建attributedString
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithFileURL:html options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
// 創建TextView,添加attributed str
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, kHeaderHeight, kScreenWidth, kScreenHeight - kHeaderHeight)];
textView.editable = NO;
[textView setAttributedText:attrStr];
[self.view addSubview:textView];
}
補充:關於 UITextView用法
//初始化並定義大小
UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(20, 10, 280, 30)];
textview.backgroundColor=[UIColor whiteColor]; //背景色
textview.scrollEnabled = NO; //當文字超過視圖的邊框時是否允許滑動,默認為“YES”
textview.editable = YES; //是否允許編輯內容,默認為“YES”
textview.delegate = self; //設置代理方法的實現類
textview.font=[UIFont fontWithName:@"Arial" size:18.0]; //設置字體名字和字體大小;
textview.returnKeyType = UIReturnKeyDefault;//return鍵的類型
textview.keyboardType = UIKeyboardTypeDefault;//鍵盤類型
textview.textAlignment = NSTextAlignmentLeft; //文本顯示的位置默認為居左
textview.dataDetectorTypes = UIDataDetectorTypeAll; //顯示數據類型的連接模式(如電話號碼、網址、地址等)
textview.textColor = [UIColor blackColor];
textview.text = @"UITextView詳解";//設置顯示的文本內容
[self.view addSubview:textview];
UITextView的代理方法如下:
//將要開始編輯
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
//將要結束編輯
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;
//開始編輯
- (void)textViewDidBeginEditing:(UITextView *)textView;
//結束編輯
- (void)textViewDidEndEditing:(UITextView *)textView;
//內容將要發生改變編輯
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
//內容發生改變編輯
- (void)textViewDidChange:(UITextView *)textView;
//焦點發生改變
- (void)textViewDidChangeSelection:(UITextView *)textView;
有時候我們要控件自適應輸入的文本的內容的高度,只要在textViewDidChange的代理方法中加入調整控件大小的代理即可
- (void)textViewDidChange:(UITextView *)textView{
//計算文本的高度
CGSize constraintSize;
constraintSize.width = textView.frame.size.width-16;
constraintSize.height = MAXFLOAT;
CGSize sizeFrame =[textView.text sizeWithFont:textView.font
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
//重新調整textView的高度
textView.frame = CGRectMake(textView.frame.origin.x,textView.frame.origin.y,textView.frame.size.width,sizeFrame.height+5);
}
控制輸入文字的長度和內容,可通調用以下代理方法實現
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (range.location>=100)
{
//控制輸入文本的長度
return NO;
}
if ([text isEqualToString:@"\n"]) {
//禁止輸入換行
return NO;
}
else
{
return YES;
}
}
UITextView退出鍵盤的幾種方式
因為iphone的軟鍵盤沒有自帶的退鍵盤鍵,所以要實現退出鍵盤需要自己實現,有如下幾種方式:
1)如果你程序是有導航條的,可以在導航條上面加多一個Done的按鈕,用來退出鍵盤,當然要先實UITextViewDelegate。
- (void)textViewDidBeginEditing:(UITextView *)textView {
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(dismissKeyBoard)];
self.navigationItem.rightBarButtonItem = done;
[done release];
done = nil;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
self.navigationItem.rightBarButtonItem = nil;
}
- (void)dismissKeyBoard {
[self.textView resignFirstResponder];
}
2)如果你的textview裡不用回車鍵,可以把回車鍵當做退出鍵盤的響應鍵。
代碼如下:
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
3)還有你也可以自定義其他加載鍵盤上面用來退出,比如在彈出的鍵盤上面加一個view來放置退出鍵盤的Done按鈕。
代碼如下:
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
[topView setBarStyle:UIBarStyleBlack];
UIBarButtonItem *btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done"
style:UIBarButtonItemStyleDone
target:self
action:@selector(dismissKeyBoard)];
NSArray * buttonsArray = @[btnSpace, doneButton];;
[doneButton release];
[btnSpace release];
[topView setItems:buttonsArray];
[textView setInputAccessoryView:topView];//當文本輸入框加上topView
[topView release];
topView = nil;
-(IBAction)dismissKeyBoard
{
[tvTextView resignFirstResponder];
}