[IOS development-17] UITextView attributes, click the add content case, and the operation method for changing the cursor/value in UITextViewDelegate, uitextviewdelegate

Source: Internet
Author: User

[IOS development-17] UITextView attributes, click the add content case, and the operation method for changing the cursor/value in UITextViewDelegate, uitextviewdelegate

(1) The difference between UITextView and UITextField can be understood as: the former is an area where many things can be written with a newline scroll bar, and the latter only has one line of text, similar to textarea and text in html.


(2) In this example, we use a UITextField and a UIButton to add the content in UITextField to the last row of UITextView. Add an event to the button and add UITextField. text to the end of UITextView. text in the event.


(3) In this example, for the user experience, you can set the cursor or text box to be the last line after each increase or decrease. The scrollRangeToVisible method is used here, this method requires a Range parameter. We can obtain the selectedRange of UITextView and pass it as a parameter. This Range series is a struct structure, including the structure of the location and length parameters, a bit similar to CGPoint and CGSize.


(4) You can also add the UITextViewDelegate protocol to AppDelegate. h to learn about the methods, including whether to allow editing, finishing editing, editing, and how to perform operations after finishing editing.


(5) There are also several protocol methods that are important, because it involves whether the content can be changed, or the position or length in seletedRange is triggered after the content is changed (that is, the position of the cursor changes and the content is increased or decreased) how to operate upon a change. This ...... Er ...... Very practical. Of course, the changed content can also be output.


# Import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController {UITextView * textView1; UITextField * textField1;}-(void) viewDidLoad {// initialize textView1 = [[UITextView alloc] init]; // set the size position textView1.frame = CGRectMake (10, 30,300,100 ); // set the background color textView1.backgroundColor = [UIColor orangeColor]; // set the text content textView1.text = @ "what shocould I say to u, my lovely world, hello, anybody home. I am Who do you know? I don't know what to enter! Are you sure you want to continue? Really? Hello, aggin, here am I. what are you doing, man? Where are u going man? "; // Set the font size, bold, italic, and other textView1.font = [UIFont boldSystemFontOfSize: 20]; // After the above settings, the text exceeds the box and we can scroll up and down to view, in addition, we can continue to input, add, delete, and so on. We can also copy and cut (double-click the text) // We can also wrap the text, and so on, this is one of the differences with textField. // if you want to edit or set it to NO, you cannot add delete or cut. You can only copy it. // The keyboard does not appear at this time, you can only double-click the text copy textView1.editable = YES; // after adding UITextViewDelegate to AppDelegate, textView1.delegate = self; [self. view addSubview: textView1]; // Add a UITextField and UIButton, enter the content in UITextField, and then click Add, add the content to UITextView textField1 = [[UITextField alloc] init]; textField1.frame = CGRectMake (10,150,300, 30); textField1.borderStyle = UITextBorderStyleRoundedRect; [self. view addSubview: textField1]; UIButton * btn1 = [UIButton buttonWithType: bytes]; btn1.frame = CGRectMake (10,200,300, 30); [btn1 setTitle: @ "add" forState: UIControlStateNormal]; btn1.backgroundColor = [UIColor orangeColor]; [self. view addSubview: btn1]; // Add an event to btn1 [btn1 addTarget: self action: @ selector (addText1) forControlEvents: UIControlEventTouchUpInside]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib .} -(void) addText1 {// note that the function used is stringByAppendingFormat textView1.text = [textView1.text stringByAppendingFormat: @ "\ n % @", textField1.text]; // after adding the parameter, the cursor is positioned at the very end. // slectedRange is a structure, indicating the position and length. The NSMakeRange behind the parameter is the same. // NSUInteger indicates an unsigned integer, however, the settings in the following two rows are invalid. Because selectedRange is an attribute, values can be obtained, values can be assigned, but not methods. Scroll actions cannot be operated. // NSUInteger len1 = textView1.text. length-1; // textView1.selectedRange = NSMakeRange (len1, 0); // you need to use the scroll operation method, and the value of selectedRange is passed as the Range value. // It is not satisfactory that it will scroll from Top to the end after each new addition. This is strange [textView1 scrollRangeToVisible: textView1.selectedRange];} // The methods in the following four protocols are the same as those in UITextFieldDelegate. Skipped: // (BOOL) skip :( UITextView *) textView; //-(BOOL) textViewShouldEndEditing :( UITextView *) textView; //-(void) textViewDidBeginEditing :( UITextView *) textView; //-(void) textViewDidEndEditing :( UITextView *) textView; // BOOL, YES, NO, it is the content that can be modified-(BOOL) textView :( UITextView *) textView shouldChangeTextInRange :( nsange) range replacementText :( NSString *) text {return YES;} // triggered only when the content changes, in addition, the modified content is valid for manual input. If you use the button in this example to add content, this operation is not triggered-(void) textViewDidChange :( UITextView *) textView {NSLog (@ "Did Change");} // almost all operations will trigger textViewDidChangeSelection, including clicking the text box and adding content to delete content // it can be understood that as long as selectedRange is involved, (location and length)-(void) textViewDidChangeSelection :( UITextView *) textView {NSLog (@ "Did Change Selection");} @ end

Screenshot:



IOS development: Get UITextView content and output txt files

NSString * textString = self. textView. text; NSLog (@ "textString is % @", textString );

How to click a button to automatically redirect the cursor to UITextView

-(Void) viewDidLoad {[super viewDidLoad]; UITextView * tt = [[UITextView alloc] initWithFrame: CGRectMake (10, 10,150,100)]; [tt setText: @ "test my TextView"]; [tt setTag: 1]; [self. view addSubview: tt]; [tt release]; UIButton * pre = [UIButton buttonWithType: UIButtonTypeRoundedRect]; [pre setFrame: CGRectMake (40,140, 60, 30)]; [pre setTitle: @ "test" forState: UIControlStateNormal]; [pre addTarget: self action: @ selector (ButtonClick) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: pre];}-(void) ButtonClick {UITextView * tt = (UITextView *) [self. view viewWithTag: 1]; [tt becomeFirstResponder];} view original post>

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.