IOS uitextview loading Local HTML file example

Source: Internet
Author: User


Sometimes we do not load a Web page directly from the server, we need to load the local HTML file, the implementation of the code is as follows:

-(void) Viewdidload {
[Super Viewdidload];
Self.view.backgroundColor = [Uicolor Whitecolor];

Create a URL from the bundle from an HTML file
Nsurl *html = [[NSBundle mainbundle] urlforresource:@ "right_obligations" withextension:@ "html"];

Create attributedstring with HTML
nsattributedstring *attrstr = [[Nsattributedstring alloc] initwithfileurl:html options:@{ Nsdocumenttypedocumentattribute:nshtmltextdocumenttype} documentattributes:nil Error:nil];

Create TextView, add attributed str
Uitextview *textview = [[Uitextview alloc] Initwithframe:cgrectmake (0, Kheaderheight, Kscreenwidth, KScreenHeight- Kheaderheight)];
textview.editable = NO;
[TextView SETATTRIBUTEDTEXT:ATTRSTR];
[Self.view Addsubview:textview];
}

Add: About Uitextview usage

Initialize and define Size

Uitextview *textview = [[Uitextview alloc] Initwithframe:cgrectmake (20, 10, 280, 30)];
Textview.backgroundcolor=[uicolor Whitecolor]; Background color
textview.scrollenabled = NO; Whether to allow sliding when text exceeds the border of the view, default to Yes
textview.editable = YES; Whether to allow editing of content, default to "YES"
Textview.delegate = self; To set the implementation class of the Proxy method
Textview.font=[uifont fontwithname:@ "Arial" size:18.0]; Set the font name and font size;
Textview.returnkeytype = type of Uireturnkeydefault;//return key
Textview.keyboardtype = uikeyboardtypedefault;//keyboard type
Textview.textalignment = Nstextalignmentleft; The text display position defaults to left
Textview.datadetectortypes = Uidatadetectortypeall; Display the connection mode for the data type (such as phone number, URL, address, etc.)
Textview.textcolor = [Uicolor blackcolor];
Textview.text = @ "Uitextview detailed";/set the text content to display
[Self.view Addsubview:textview];

The Uitextview proxy method is as follows:
Going to start editing
-(BOOL) textviewshouldbeginediting: (Uitextview *) TextView;

will end Edit
-(BOOL) textviewshouldendediting: (Uitextview *) TextView;

Start editing
-(void) textviewdidbeginediting: (Uitextview *) TextView;

End Edit
-(void) textviewdidendediting: (Uitextview *) TextView;

Content is going to change edit
-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) text;

Content changes Edit
-(void) Textviewdidchange: (Uitextview *) TextView;

The focus has changed.
-(void) Textviewdidchangeselection: (Uitextview *) TextView;

Sometimes we want the control to adapt to the height of the content of the input text, as long as you add a proxy that resizes the control to the Textviewdidchange proxy method


-(void) Textviewdidchange: (Uitextview *) textview{
Calculate the height of the text
Cgsize constraintsize;
Constraintsize.width = textview.frame.size.width-16;
Constraintsize.height = maxfloat;
Cgsize sizeframe =[textview.text SizeWithFont:textView.font
Constrainedtosize:constraintsize
Linebreakmode:uilinebreakmodewordwrap];

Readjust the height of the TextView
Textview.frame = CGRectMake (Textview.frame.origin.x,textview.frame.origin.y,textview.frame.size.width, SIZEFRAME.HEIGHT+5);
}


Control the length and content of the input text, the following proxy method can be invoked to implement
-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) text
{
if (range.location>=100)
{
Control the length of the input text
return NO;
}
if ([text isequaltostring:@ "\ n"]) {
Prevent input line wrapping
return NO;
}
Else
{
return YES;
}
}


Uitextview exit the keyboard in several ways
Because the iphone's soft keyboard does not have its own backspace keyboard, so to achieve the exit keyboard needs to implement their own, there are several ways:
1 If your program has a navigation bar, you can add more than one done button on the navigation bar, used to exit the keyboard, of course, first real 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 If you do not use enter in the TextView, you can use the return key as a response to exit the keyboard.
The code is as follows:

-(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) text
{
if ([text isequaltostring:@ "\ n"]) {
[TextView Resignfirstresponder];
return NO;
}
return YES;
}


3 and you can also customize the other loaded keyboard to exit, such as a pop-up keyboard above a view to put the quit the keyboard done button.
The code is as follows:


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];//when text input box plus TopView
[TopView release];
TopView = nil;

-(Ibaction) Dismisskeyboard
{
[Tvtextview Resignfirstresponder];
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.