I want to change cursor position in textview of SLComposeViewController to beginning of UITextView
currently is show me the end of initial Text:
Currently Cursor
but I want cursor at first position in UITextView. with (iOS 9 support)
I also use this code
[self presentViewController:sharingComposer animated:NO completion:^{
for (UIView *viewLayer1 in sharingComposer.view.subviews) {
for (UIView *viewLayer2 in viewLayer1.subviews) {
if ([viewLayer2 isKindOfClass:[UIView class]]) {
for (UIView *viewLayer3 in viewLayer2.subviews) {
if ([viewLayer3 isKindOfClass:[UITextView class]]) {
[(UITextView *)viewLayer3 setDelegate:self];
sharingTextView = (UITextView *)viewLayer3;
}
}
}
}
}
}];
but i can't get sharing UITextView and not working for me.
Try this:-
UITextPosition *beginning = [sharingTextView beginningOfDocument];
[sharingTextView setSelectedTextRange:[textField textRangeFromPosition:beginning
toPosition:beginning]];
Related
I have used this piece of code to set Text Color for the Texfield Inside the UISearchBar.
for (UIView* view in subViews)
{
if([view isKindOfClass:[UITextField class]])
{
UITextField* searchTextField = (UITextField*)view;
[self setTextColorToTextField:searchTextField];
break;
}
else
{
for (id view1 in view.subviews)
{
if ([view1 isKindOfClass:[UITextField class]])
{
UITextField* searchTextField = (UITextField*)view1;
[self setTextColorToTextField:searchTextField];
break;
}
}
}
}
Can anyone suggest how to improve the code?
Well there is no need to code when you can set its colour in the StoryBoard.
Here add this line in identity inspector of searchBar.
I have a text field that I want to have a keyboard like this when user start typing:
please also see this video: https://youtu.be/iU_jocny3N0
As you can see in this video there is a "ABC" key that helps user to switch from number pad to text. and also when press "123" in text the keyboard switchs from text to number pad. I am wondering how they do this?
The only solution that I found was adding a subview to keyboard like what described here:
Adding Done Button to Only Number Pad Keyboard on iPhone
but this way may not work when user uses custom keyboards. and also do not works for switching from text to number pad.
Or as another solution I know accessoryInputView but this is not like the video. It adds a toolbar above the keyboard.
Does someone knows the solutions that is used in this video?
I have added comma button to the keyboard,
Keyboard is also a simple UIView Which contains Controls
NOTE: This is old code was working in my old project Not tested in new projects
- (void) keyboardWillShow:(NSNotification *)note {
// create custom button
dispatch_async(dispatch_get_main_queue(), ^{
// Some code
UITextField *txt = (UITextField *)[self.view findFirstResponder];
if (txt.keyboardType == UIKeyboardTypeDecimalPad) {
UIButton * btnComma = [UIButton buttonWithType:UIButtonTypeCustom];
[btnComma setTag:15000];
UIView* keyboard = [self findKeyboard];
// btnComma.frame = CGRectMake(0, 162, 126, 54);
btnComma.frame = [self findKeySizeForView:keyboard];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"8.0")) {
[btnComma setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 20, 0)];
}
[btnComma setBackgroundColor:[UIColor colorWithHexString:#"CBD0D6"]];
btnComma.adjustsImageWhenHighlighted = NO;
[btnComma setTitle:#"." forState:UIControlStateNormal];
[btnComma setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnComma.titleLabel setFont:[UIFont systemFontOfSize:35.0f]];
[btnComma addTarget:self action:#selector(commaBtnTapped) forControlEvents:UIControlEventTouchUpInside];
[keyboard addSubview:btnComma];
btnComma = nil;
}
});
}
- (UIView *) viewWithPrefix:(NSString *)prefix inView:(UIView *)view {
for (UIView *subview in view.subviews) {
if ([[subview description] hasPrefix:prefix]) {
return subview;
}
}
return nil;
}
This method for finding keyboard from UIWindow
- (UIView *) findKeyboard {
for (UIWindow* window in [UIApplication sharedApplication].windows) {
UIView *inputSetContainer = [self viewWithPrefix:#"<UIInputSetContainerView" inView:window];
if (inputSetContainer) {
UIView *inputSetHost = [self viewWithPrefix:#"<UIInputSetHostView" inView:inputSetContainer];
if (inputSetHost) {
UIView *kbinputbackdrop = [self viewWithPrefix:#"<_UIKBCompatInput" inView:inputSetHost];
if (kbinputbackdrop) {
UIView *theKeyboard = [self viewWithPrefix:#"<UIKeyboard" inView:kbinputbackdrop];
return theKeyboard;
}
}
}
}
return nil;
}
and For finding size of bottom right button
- (CGRect ) findKeySizeForView:(UIView *)view {
if (view != nil) {
UIView *uiKeyboardImpl = [self viewWithPrefix:#"<UIKeyboardImpl" inView:view];
if (uiKeyboardImpl != nil) {
UIView *uiKeyboardLayoutStar = [self viewWithPrefix:#"<UIKeyboardLayoutStar" inView:uiKeyboardImpl];
if (uiKeyboardLayoutStar != nil) {
UIView *uiKBKeyplaneView = [self viewWithPrefix:#"<UIKBKeyplaneView" inView:uiKeyboardLayoutStar];
if (uiKBKeyplaneView != nil) {
for (view in [uiKBKeyplaneView subviews]) {
CGPoint pointOrigin = view.layer.frame.origin;
if (pointOrigin.x <= 0 && pointOrigin.y == uiKBKeyplaneView.frame.size.height - view.frame.size.height && [[view description] hasPrefix:#"<UIKBKeyView"])
return view.layer.frame;
}
}
}
}
}
return CGRectZero;
}
I have been attempting to hide a UISearchBar keyboard but my code fails to run successfully. I have the following code which removes the cursor but the keyboard continues to show:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder]; // using method search bar
[theSearchBar resignFirstResponder]; // using actual object name
[self.view endEditing:YES];
}
I have set the delegate in the .h as <UISearchBarDelegate> and set searchBar.delegate = self; in .m
Any ideas? Thanks.
Try to use following methods for search an resign first responder:
- (UIView *)findFirstResponderBeneathView:(UIView *)view {
for ( UIView *childView in view.subviews ) {
if ( [childView respondsToSelector:#selector(isFirstResponder)] && [childView isFirstResponder] ) return childView;
UIView *result = [self findFirstResponderBeneathView:childView];
if ( result ) return result;
}
return nil;
}
- (void)hideKeyboard {
[[self findFirstResponderBeneathView:self.view] resignFirstResponder];
}
I created a search bar programmatically and added to my view using the codes below:
- (void)viewDidLoad
{
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(xPositionForSearchBar, yPositionForSearchBar, widthForSearchBar, heightForSearchBar)];
UIView *bg = [[searchBar subviews] objectAtIndex:0];
searchBar.delegate = self;
searchBar.placeholder = #"Search record";
for(UIView *view in searchBar.subviews){
if([view isKindOfClass:[UITextField class]]){
UITextField *tf = (UITextField *)view;
tf.delegate = self;
break;
}
}
[bg removeFromSuperview];
[self.view addSubview: searchBar];
}
The code is implemented with UISearchBarDelegate and UITextFieldDelegate.
I have tried using
- (void)searchBarCancelButtonClicked:(UISearchBar *)aSearchBar
{
NSLog(#"cancel clicked");
searchBar.text = #"";
[aSearchBar resignFirstResponder];
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
NSLog(#"clear");
[self performSelector:#selector(searchBarCancelButtonClicked:) withObject:searchBar afterDelay: 0.1];
return YES;
}
and yet, the text inside the searchBar is not cleared at all when i click on the "clear button" - a circle with a "X" inside.
The clear button works when I implemented it in IB. Wonder why?
Kindly advice, many thanks.
This might happen if you position your search bar out of the bounds of a parent view. Then, the touches aren't delivered to your searchBar properly. This also affects text editing controls like copy & paste.
I have checked your code it work fine on device as well as on simulator.
i have placed Dynamically created textfield in scrollview.I need to set contentOffset for the scrollview. In textfield shouldbegin Editing i have given the scrollview. It works fine if i traverse through Done button. But in betwwen if i tap any textfield that textfield goes up and and i cannot see the textfield. It shows the nextfield offset but cursor is in correct textfield. My code is
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(enteredHealthyHeart)
{
for(int i=0;i<[healthyHeart_TxtFldAry count];i++)
{
if(textField == [healthyHeart_TxtFldAry objectAtIndex:i])
{
getHHTag=textField.tag;
NSLog(#"getHHTag %i",getHHTag);
UITextField *tempTxtFld=[healthyHeart_TxtFldAry objectAtIndex:getHHTag];
if(i<([healthyHeart_TxtFldAry count]-1))
{
int j =tempTxtFld.frame.origin.y;
healthyHeartScrollView.contentOffset=CGPointMake(0 , j);
NSLog(#"j>>>>>%i",j);
}
if(i==([healthyHeart_TxtFldAry count]-1))
{
healthyHeartScrollView.contentOffset=CGPointMake(0 ,tempTxtFld.frame.origin.y);
}
[tempTxtFld resignFirstResponder];
return YES;
}
}
}
}
Please help me to solve this issue.
Firstly add tag to each and every UITextField;
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//Take reference of all UITextField u added let say u have 3;
UITextField *txt = (UItextField *)[self.view viewWithTag:99];
UITextField *txt1 = (UItextField *)[self.view viewWithTag:199];
UITextField *txt2 = (UItextField *)[self.view viewWithTag:299];
if(textField == txt)
{
[txt1 resignFirstResponder];
[txt2 resignFirstResponder];
//required scroll view offset here
}
if(textField == txt1)
{
[txt resignFirstResponder];
[txt2 resignFirstResponder];
//required scroll view offset here
}
if(textField == txt2)
{
[txt resignFirstResponder];
[txt1 resignFirstResponder];
//required scroll view offset here
}
return YES;
}
scrollview.contentOffset is the point at which the content view is offset from the orgin scroll view. In you code
healthyHeartScrollView.contentOffset=CGPointMake(0 ,tempTxtFld.frame.origin.y);
The tempTxtFld is out of hte content view of healthyHeartScrollView. This makes the tempTxtFld.frame.orgin.y be negative relative to the contentview of healthyHeartScrollView.
So you can't see it.
ps. I really do not know what you want to do.-_-