How to get highlighted text of UITextView in UITableViewCell? - uitableview

I have a custom cell (subclass of UITableViewCell) with a textView inside of it. It works great! Now, when I tap on a cell and highlight some text, the default UIMenuController appears and I can choose to copy the highlighted text. Also this function works perfectly. Now, I would like to add a custom button to the UIMenuController, which I actually did, but to perform the menu item action I need to know what the selected text is. How can I get it?

To explain this better, there is no method in UITextField that allows us to know what the currently selected text is. But we can leverage the copy action on the text field that is associated with the menu controller. The copy action copies the text onto the pasteboard which we will need to retrieve. I was able to implement a Log function in my custom subclass of UITextField like this –
- (void)log:(id)sender {
[self copy:sender];
NSString *highlightedText = [UIPasteboard generalPasteboard].string;
NSLog(#"%#", highlightedText);
}
This logs the selected text onto the console. Doesn't do much but gives you the basic idea.

Related

How can I detect that a user has tapped a formatting button in a UIMenuController?

My app allows users to format text in a UITextView by using some custom formatting buttons I've created. Now I want to enable the allowsEditingTextAttributes property of the text view so that users can copy and paste formatted text between text views. This enables the bold/italic/underline buttons in the standard UIMenuController for the text view. This is fine, but when users change the formatting through this menu, I need to call my custom method that updates the state of my custom formatting buttons. How can I detect that a user has tapped one of the UIMenuController's formatting buttons?
I looked for notifications in the UITextView and UIMenuController class references, but didn't see anything relevant. These formatting buttons don't trigger a UITextViewTextDidChangeNotification and don't trigger the textViewDidChange: method of the UITextViewDelegate. I also tried subclassing the UITextView class to override a method there, but I don't know what method to override. It looks like a bold: method doesn't exist, for example, even though a paste: method does exist there. Does anyone know what method of what class is called when tapping one of the bold/italic/underline buttons?
These buttons aren't mentioned in the Text Programming Guide for iOS or the UIMenuController class reference, as far as I can tell.
This might be more helpful.
If you implement this method in UITextView subclass then you can control whether the menu options display or not. You can also check the log output to see what methods are getting called. In the example below I am disabling the text style options menu.
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
LOG(#"canPerformAction: called");
FLOG(#" action %#", NSStringFromSelector(action));
if (action == #selector(_showTextStyleOptions:)) {
return NO;
}
return [super canPerformAction:action withSender:sender];
}
These are some of the selectors that get checked:
copy:
select:
selectAll:
paste:
delete:
_promptForReplace:
_showTextStyleOptions: ***
_define:
etc...
If you select the Text Style Options menu then you get the following:
toggleBoldface:
toggleItalics:
toggleUnderline:
Hope this helps.

How to add a rectangular layout on string in UITextfield with a clear button in iOS

I want to do something very similar to email address textfield in iOS. Just like email address gets highlighted in a different colour and has a clear button next to it. On clicking the clear button that entire email address should get deleted.Attaching a picture of that. Can you please give me some pointers? I know how to use NSAttributedString to highlight the text but no idea how to add the clear button next to it. Thank you!
To do this I would subclass a UIView object say for example call it emailAddressView with an associated nib file where you add a UILabel for the name and a UIButton for the 'x' button.
You could then add this view as a subview to your UITextfield as the user enters in valid entries.
add a UIButton as subview of your UITextView

detect urls in UILabel

I have a Tableview and tableview cell is customized to have a UILabel. The text in UILabel is having URLs. Is there a way to detect urls like how UITextView will enable detect URLs so that user interaction should be able to load the urls.
If you just want to identify the URLs, you can use NSDataDetector with the NSTextCheckingTypeLink checking type.
If you want to draw the URLs differently, and you are targeting iOS 6, you can use an NSAttributedString to turn the URLs blue or underline them or whatever. If you're targeting an older version of iOS, you will probably want to look for some free code on the Internet to draw styled text, like OHAttributedLabel.
If you want to actually make the URLs touch-sensitive, you can add a tap gesture recognizer to the label and try to figure out which part of the string was tapped (somewhat complicated), or look for some free code on the Internet that already does it for you (like OHAttributedLabel), or just put a UITextView in the table view cell instead of a label.
as Rob point out how can we achieved the same is awesome.
But, we can use a tact so can save with issue of ios version (possibly), just by using a UILabel and UIButton.
What we need to do is that, either from IB or story-board,just place a UILabel with string as URL(use this as title), say;
"www.myURL.com"
Now, just above it, place a UIButton(button with custom type), and just use "______"(underline) and set this button as overlap your UILabel and also the 'underline' must be beneath your label.
Now, just do in action of button whatever you required as you need on the click of URL and here also you can change the textColor, etc, properties; also load URL and navigate to UIWebView.

resignfirstresponder on a UITextView inside a UITableViewCell

I am hoping if someone can help me resolve an IOS/XCode question.
I need to have a UITextView created inside a UITableViewCell, this UITextView has responds to a user click, upon which a UIPopoverController will be displayed so that a sub-UITableView is displayed (inside the UIPopoverController) allowing a user to select from a list of choices (lines of text). After the user select the choice (one of the line of text), that line of text will then be displayed inside the said UITextView. First problem I am having is that when the user click on the UITextView the keyboard gets displayed instead of the UIPopoverController. How do I go about disabling ie. calling resignFirstResponder so that instead of the keyboard displaying, I get the UIPopoverController coming up instead. Would someone be kind enough to share similar codes? or point me to some sample of how this can be done? Thanks so much in advance.
You can use following delegate method to detect when textView is tapped and show your popOverController accordingly, return 'NO' in the delegate method so that no keyboard will appear...
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
// code to show popOverController
return NO;
}

How do I make the keyboard go away when the user clicks somewhere else? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Dismiss keyboard by touching background of UITableView
How do I make the keyboard go away when the user clicks somewhere else?
Note: I know how to make the keyboard disappear with sending the resignFirstResponder command to the UITextField. At present the "Done" button is connected to all the correct code to do this and this works.
I have a UITableView with different UITableViewCells, and if the user moves onto another cell I want the keyboard to disappear.
So what events do I also need to include the resignFirstResponder in, for the keyboard to disappear.
Suppose UITableViewCell A has the UITextField, and UITableViewCell B has a button. If the user presses the button in cell B, then I will need to send the command resignFirstResponder back to the UITextField in cell A. First of all the button has no idea which cell it should sent the command to, and second even if the button did know which cell to send the command to how would it?
There's no trivial way to do this. You can put a transparent set of "shield views" all the way around the text field that take up the rest of the screen, and use any touches on them to dismiss the keyboard.
You can create a generic 'hideKeyboard' method in which you can include all text fields that can be first responders. For example,
-(void) hideKeyboard {
[textFieldName resignFirstResponder];
[textFieldSurname resignFirstResponder];
for (UITextField * txtField in arrTextFields) {
[txtField resignFirstResponder];
}
}
Then, at various sections in your class, depending on the functionality required, call;
[self hideKeyBoard];
This simple method means you won't need to keep track of the individual item that 'has the focus' / first responder status.
How to touch any part of the screen to make the keyboard go away
To touch somewhere outside the UITableView and have the keyboard disappear, place an invisible button on top of the 'touch area' that you want to respond to. Then, simply call [self hideKeyboard] from the touch event for that invisible button. Using IB, drag a new rounded button onto your view, then size it to take up the full size of the screen. Next,drag the button up or down the controls list in the IB document window so that button is behind all text fields and buttons, but in front of anything else (like images etc.). Finally, change the type of the button to 'Custom' to make it invisible, but still respond to events. Now all you have to do is to connect the new button's 'touch up inside' event to trigger the 'hideKeyboard' method.
Additionally, see this post for a brilliant solution to dismiss the keyboard when the above solution doesn't work : stackoverflow question 1823317

Resources