textView.selectedRange = NSMakeRange(0, 0);
This has no effect on where the cursor is placed.
I then used a breakpoint and typed in po textView.selectedRange in the debugger.
The result was:
(lldb) property 'selectedRange' not found on object of type
'UITextView *'
Since UITextView inherits from UIResponder. So, you can call the -becomeFirstResponder method on your text view, which will cause it to become the first responder and begin editing:
[textView becomeFirstResponder];
After that, you can selectedRange of UITextView.
[textView setSelectedRange:NSMakeRange(0, 10)];
Steps
Added the UITextView in Story board view controller.
Added the UITextView delegate in View controller.
#interface SecondViewController : UIViewController<UITextViewDelegate>
Assign the property to text view.
#property (strong, nonatomic) IBOutlet UITextView *textView;
And selected the text range in textview.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.textView becomeFirstResponder];
[self.textView setSelectedRange:NSMakeRange(0, 10)];
}
Related
I have four UITextFields and associated each with an outlet in .h files. I have also defined 4 dismiss function for each textfield as shown below:
- (IBAction)dismiss1:(id)sender;
- (IBAction)dismiss2:(id)sender;
- (IBAction)dismiss3:(id)sender;
- (IBAction)dismiss4:(id)sender;
#property (strong, nonatomic) IBOutlet UITextField *name;
#property (strong, nonatomic) IBOutlet UITextField *email;
#property (strong, nonatomic) IBOutlet UITextField *weight;
#property (strong, nonatomic) IBOutlet UITextField *age;
Implementation of dismiss function in the .m file:
- (IBAction)dismiss1:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)dismiss2:(id)sender {
[sender resignFirstResponder];
}
...
I am very sure that the outlet is connected to each UITextFiled correctly. The IBAction of each dismiss function is also connected with 'Editing did end' event correspondingly. However, when I ran the app using simulator, the keyboard will not dismiss when I click 'Enter/Done'. It's also very weird that when I place the breakpoint inside the dismiss function, clicking 'Enter' when typing in the corresponding UITextField does not bring up the debugger.
Thanks a lot for helping!
Update: I checked the object type of sender (dismiss1) using breakpoint and it's UITextFiled. However, I did not entered the debugging mode when I click 'Enter' in the first TextField, but entered the debugging mode when I click on the second TextField (before typing).
set UITextFielDelegate
textfieldname.delegate = self;
use this code it will resolve your issue
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Use the code to resign keyBoard without specify textFiled name.
- (IBAction)dismiss1:(UITextField*)sender {
[self.view endEditing:true];
// or use [sender resignFirstResponder];
}
Add text field delegate
name.delegate = self;
email.delegate = self;
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
No need to create four actions for textfield
You can set delegate of Textfield and use following method
- (void)textFieldDidEndEditing:(UITextField *)textField
Inside this method you can compare your textfield with method returning textfield and get you task done in "if" condition
Hope it help you!!
Assign the tag to different textfields
then >
textfield.delegate = self;
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textfield.tag==num)
{
[textField resignFirstResponder];
}
return YES;
}
The Editing did end is called when a textfield did lose the "focus", mean it is not the first responder any more (because it has resigned or an other one became first responder).
Use textFieldShouldReturn to detect when the return key is called.
And call resignFirstResponder on the texfield when this is called. The Editing did end event will occur after that.
Edit : you have to implement something (Your ViewController?) as delegate of the textfield.
How Can I copy UITextField string to UITextView?
I want to do it when UIButton;
[myUIButton addTarget:self action:#selector(touchButton) forControlEvents:UIControlEventTouchUpInside];
UITextField* textField (initialized, omit code here)
[self.view addSubview:textField];
//save string to the property
self.textFieldString = textField.text;
//textFieldString is #property NSString* textFieldString; at the header.
UITextView* textView (initialized, omit code here)
[self.textView setEditable:false];
[self.view addSubview:self.textView];
//here i want to implement UITextField string -> UITextView display
-(void)submitButtonClicked {
//....
//Problem i am having here is that, I can not see instance variable other than #property variable. How should I pass UITextField .text to UITextView?
}
Prepare the identifier to each UI
[textField setTag:100];
[self.view addSubview:textField];
[textView setTag:200];
[self.view addSubview:textView];
Identify the each UI element and Manage
-(void)submitButtonClicked {
//Problem i am having here is that,
// I can not see instance variable other than #property variable.
// How should I pass UITextField .text to UITextView?
UITextField *myTextField=(UITextField*)[self.view viewWithTag:100];
UITextView *myTextView=(UITextView*)[self.view viewWithTag:200];
myTextView.text=myTextField.text;
}
If you are creating UITextView programatically then create a property variable and synthesize the same. You can access the same using the synthesized name and in UIButton action method get the text and set it to UITextView.
In .m file you can declare UITextView as
#interface classname () {
UITextView *textView
}
or in .h file
#property (nonatomic, strong) UITextView *textView;
As you have described the problem 'Problem i am having here is that, I can not see instance variable other than #property variable. How should I pass UITextField .text to UITextView?'
In your button's action use:
textView.text=textField.text;
Declare your variables between #interface and #end and initialize them in the viewDidLoad. This way you'll be able to use them in your button's action.
#interface ViewController ()
{
UITextView *textView;
UITextField *textField;
}
#implementation ViewController
-(void) viewDidLoad
{
// Do the following
// Initialize both textView and textField
// Set their frames
// Add both of them as a subview to your view
}
#end
Now you'll be able to access both of them in your button's action.
Hope this helps.
As you can see on UITextView class, linkTextAttributes seems to be a new property available from iOS7:
// Style for links
#property(nonatomic, copy) NSDictionary *linkTextAttributes NS_AVAILABLE_IOS(7_0);
and it should color links differently in an UITextView instance. So I tried to put a static (not editable) UITextView in a view controller (child of a tab bar controller), and set this property like below:
#property (nonatomic,strong) IBOutlet UITextView *copyrightText;
- (void)viewDidLoad
{
[super viewDidLoad];
UIColor *linkColor = [UIColor colorWithRed:202.0f/255.0f green:202.0f/255.0f blue:202.0f/255.0f alpha:1];
NSDictionary *attributes = #{NSForegroundColorAttributeName:linkColor};
self.copyrightText.linkTextAttributes = attributes;
}
but at first load, links color seems to be not set. Then, if I switch to another VC and return to current VC, links color changes. What's the problem with this code?
You can try this line of code. I'm always using this with the animation. I think it can help you achieve that view in first load.
------> [self.view layoutIfNeeded];
I have few text fields in a UIViewController. For some of the text fields I have used IBOutlet to make property. For some textFields that I add programmatically (since they are in scroll view, they are not in the constraints of the view controller window in soryboard. ) , I have just made them property without IBOutlet.
So for example, I have:
#property (weak, nonatomic) **IBOutlet** UITextField *descriptionTextBox;
#property(strong , nonatomic) UITextField *cityTextField;
Now I set the delegates of both in ViewDidLoad & also in .h file <UITextFieldDelegate>
But after implementing - (BOOL)textFieldShouldReturn:(UITextField *)textField method , the keyboard only return for the text field having IBOutlet.What can I do for it?
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder]; //works only for text field declared in storyBoard.
return YES;
}
In your view controller, say [self.view endEditing:YES]. This will dismiss the keyboard regardless of who is first responder.
I think it would be helpful to see more code.
However your issue could be that you're not setting the delegate on your UITextFields hence the method only being called on the one in the storyboard.
Check to see whether you have set delegate to your UITextField objects that were created programatically.
I have UITextFields in tableviewcells. When you swipe over the cell not part of the textfield, the delete action comes up as expected. If you swipe over the textfield, it stops the delete from popping up.
How do I fix this so that you can swipe over the inputs and the cell will trigger the delete action?
I think the issue here is that the touch on the text field is interfering with your swipe gesture recognizer (presumably attached to the parent view). I had a similar problem with a text field that was placed in a UIScrollView.
I got around this problem by overlaying a clear UIView on top of my UITextField. I then assigned a UITapGestureRecognizer to this clear view to set the text field as the first responder when the user taps on the field. Otherwise, the swiped is sent to the parent view (a scroll view) which recognizes the swipe without any issues. It's kind of lame but it works.
This scenario is a bit different from yours, but I think it's the same problem. Here is what my code looks like, hopefully it helps:
// UIView subclass header
#interface LSAddPageView : UIView
#property (weak, nonatomic) IBOutlet UITextField *textField; // Connected to the UITextField in question
#property (strong, nonatomic) UIView *textFieldMask;
#property (assign, nonatomic) BOOL textFieldMaskEnabled;
#end
// UIView subclass implementation
#implementation LSAddPageView
- (void)awakeFromNib
{
[super awakeFromNib];
_textFieldMask = [UIView new];
_textFieldMask.backgroundColor = [UIColor clearColor];
[self insertSubview:_textFieldMask aboveSubview:self.textField];
}
- (void)layoutSubviews
{
[super layoutSubviews];
_textFieldMask.frame = self.textField.frame;
}
- (BOOL)textFieldMaskEnabled
{
return _textFieldMask.hidden == NO;
}
- (void)setTextFieldMaskEnabled:(BOOL)textFieldMaskEnabled
{
_textFieldMask.hidden = !textFieldMaskEnabled;
}
#end
And then in the controller:
- (void)viewDidLoad
{
[super viewDidLoad];
_addPageView = (LSAddPageView*)self.view;
_maskGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didTapMask:)];
_maskGestureRecognizer.numberOfTapsRequired = 1;
_maskGestureRecognizer.numberOfTouchesRequired = 1;
[_addPageView.textFieldMask addGestureRecognizer:_maskGestureRecognizer];
self.textField.delegate = self; // Set delegate to be notified when text field resigns first responder
}
- (void)didTapMask:(UIGestureRecognizer*)recognizer
{
_addPageView.textFieldMaskEnabled = NO;
[self.textField becomeFirstResponder];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
_addPageView.textFieldMaskEnabled = YES;
return YES;
}
Sounds like you need to set the cancelsTouchesInView property
yourGestureRecognizer.cancelsTouchesInView = NO;
from UIButton & UITextField will block UITableViewCell being swipe to delete
self.tableView.panGestureRecognizer.delaysTouchesBegan = YES;
this will make the textfield not stop left swipe guestures