I'm working on a very small editor in iOS. It only has one view, one button and a textView. When the button gets pressed, a custom UIMenuController pops up with 3 options: toggle bold, toggle italics and toggle cursive.
This is working very well, however, if I press the button when the UITextView is the first responder, it also shows the two default menu items, named 'select' and 'select all'.
I want to get rid of them but I'm not sure how to do this. This is the code that gets called when the button is pressed:
- (IBAction)settingsPressed:(id)sender
{
UIMenuController *sharedController = [UIMenuController sharedMenuController];
UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:bold ? #"Bold off" : #"Bold on" action:#selector(toggleBold:)];
UIMenuItem *menuItem2 = [[UIMenuItem alloc] initWithTitle:italics ? #"Italics off" : #"Italics on" action:#selector(toggleCursive:)];
UIMenuItem *menuItem3 = [[UIMenuItem alloc] initWithTitle:underline ? #"Underline off" : #"Underline on" action:#selector(toggleUnderline:)];
NSArray *menuItems = #[menuItem1, menuItem2, menuItem3];
CGRect drawRect = [sender convertRect:[sender bounds] toView:self.view];
[sharedController setTargetRect:drawRect inView:self.view];
[sharedController setMenuItems:menuItems];
[sharedController setMenuVisible:YES animated:YES];
[sharedController setMenuItems:nil];
}
Can anyone explain me how to do this?
Thanks!
Make a subclass of UITextView. In your subclass, override canPerformAction:withSender: to return NO if the action is #selector(select:) or #selector(selectAll:). For more information:
Managing the Selection and Edit Menu
UIMenuController Class Reference
-[UIResponder canPerformAction:withSender:]
UIResponderStandardEditActions Protocol Reference
Related
I have a tableview cell that has an image view, a UILabel and a TextView. I want the user to be able to select text from the TextView when they longPress on the TextView.
I have created a Gesture recogniser for the cell, and when the user long presses on the cell, the gesture recogniser gets called, but I don't get a menu or a cursor for the user to be able to select text and then copy it. Below is the code to attach long press to the UITableView cell
UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(LongPressgesture:)];
[tableView addGestureRecognizer:longPressRecognizer];
I also have the following code that gets called
- (void)LongPressgesture:(UILongPressGestureRecognizer *)gesture{
if (gesture.state == UIGestureRecognizerStateEnded) {
NSLog(#"Long press Ended .................");
}
else {
NSLog(#"Long press detected .....................");
}
}
The function gets called, so I see the message "Long press Detected" on the output log, but I don't get the menu to copy text. I tried creating a menu on the Long Press section as this:
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:#"Copy" action:#selector(copyText:)];
UIMenuController *menuCont = [UIMenuController sharedMenuController];
[menuCont setTargetRect:CGRectMake(10, 100, 400, 400) inView:tableview];
menuCont.arrowDirection = UIMenuControllerArrowDown;
menuCont.menuItems = [NSArray arrayWithObject:menuItem];
[menuCont setMenuVisible:YES animated:YES];
But the copy text function is not called, and there is no menu.
Thanks!
I am trying to create editable transparent NSTextField in a semi transparent window:
What I have noticed is that whenever the field is editable there is a white "selection like" background drawn even though the element is not actually selected.
Additional observable symptoms:
This highlight is not present when the field is set as non-editable.
If there are multiple fields only the first one has the highlight.
The highlight is not present if the text is not set programmatically
Following code was used to generate the field:
f = [[NSTextField alloc] initWithFrame:b2];
f.backgroundColor = [NSColor clearColor];
f.drawsBackground = YES;
f.bordered = NO;
f.bezeled = NO;
f.focusRingType = NSFocusRingTypeNone;
f.textColor = [NSColor whiteColor];
f.editable = YES;
f.selectable = YES;
f.backgroundColor = [NSColor clearColor];
f.allowsEditingTextAttributes = YES;
f.stringValue = #"Foo";
[self.contentView addSubview:f];
Additional observations (potentially a separate problem):
When field is not the first field on the screen and the initial text is set programmatically and removed by editing the field there is a shadow of the text:
I can't seem to find any documentation on this I wonder if any of you have had this happen and potentially have a solution or a pointer to docs I might have not stumbled upon.
part 1: removing highlight
there are two options here depending on the behavior you are looking for
option 1 - nil first responder
TextField is not first responder
No highlighted text
No Cursor at the end of text
Assuming you are using an NSWindow, set the first responder to nil after calling makeKeyAndOrderFront
[self.window makeKeyAndOrderFront:self];
[self.window makeFirstResponder:nil];
It appears as though makeKeyAndOrderToFront: looks for the first NSResponder in the window willing to accept first responder. Then becomeFirstResponder is called on that responder; leading to option 2
option 2 - override becomeFirstResponder
TextField is first responder
No highlighted text
Cursor appears at the trailing edge of text
Subclass NSTextfield and override it's becomeFirstResponder method
#implementation BPTextField
- (BOOL)becomeFirstResponder {
BOOL isResponder = [super becomeFirstResponder];
//Get Field editor, set selected range
NSText* fieldEditor = [[self window] fieldEditor:YES forObject:self];
[fieldEditor setSelectedRange:NSMakeRange(fieldEditor.string.length ,0)];
return isResponder;
}
#end
I prefer this option from a usability perspective
part 2: removing shadow
option 1 - add a solid background color
I'm not clear ; ) on why this is the case, but if you add a solid background color, the text will update.
option 2 - override textDidChange
override textDidChange:notification in your textfield
#implementation BPTextField
- (void)textDidChange:(NSNotification *)notification {
[super textDidChange:notification];
[self setNeedsDisplay:YES];
}
#end
Final notes
You'll notice that the text looks bad, or rigid. Adding a background color to the textfield, or to the superview's layer will fix this.
This is an answer to part 2 of the question.
The shadow artifact is from rendering window's shadow which is not updated when the text in the NSTextField changes.
If the window's hasShadow method returns "NO" the text's shadow will not create shadow for the text either.
The UITextFields in my app have placeholder text defined (in Interface Builder), and I cannot cause these fields to acquire focus (i.e. show the keyboard and allow editing) when I tap on the area occupied by the placeholder text. If I tap on the textfields in an area just outside the that of placeholder text (though still within the bounds of the textfiled itself), it acts as normal (i.e. the keyboard pops up and I can edit the content of the textfield). How can I fix this?
Thanks.
EDIT 1
Ok, I think I've got it. I'm also setting a blank view to the "leftView" property of these UITextFields. If I remove this, you can touch the UITextFields in the area of the placeholder text and it reacts as expected; I need this view for the leftView though. If you change the background color of this spacer view to red, you can see that it doesn't get in the way at all, so I don't know what's going wrong.
Why does this code cause this problem?
Thanks.
+(UIView*)getTextFieldLeftSpacerViewWithBackgroundColor:(UIColor*)backgroundColor andHeight:(CGFloat)height
{
UIView *leftWrapper = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 8.0f, height)];
leftWrapper.autoresizingMask = UIViewAutoresizingNone;
[leftWrapper setOpaque:YES];
if(backgroundColor){leftWrapper.backgroundColor = backgroundColor;}
else{leftWrapper.backgroundColor = [UIColor clearColor];}
return [leftWrapper autorelease];
}
+(void)setTextFieldLeftSpacerForTextFieled:(UITextField*)textField
{
if(textField)
{
UIView *spacer = [MYViewController getTextFieldLeftSpacerViewWithBackgroundColor:nil andHeight:textField.bounds.size.height];
textField.leftView = spacer;
textField.leftViewMode = UITextFieldViewModeAlways;
}
}
Just ran into the same problem and didn't want to subclass, just had to use :
leftWrapper.userInteractionEnabled = NO;
I abandoned this approach. Instead of using an invisible view to offset the text, I opted to subclass UITextField and provide offset CGRects for the bounds of the text within theUITextField. The following SO post was very helpful:
Indent the text in a UITextField
I have root ViewController and detailed ViewController. When i push to detailedViewController i get leftBarButtonItem with the title from the root one. But i want the title to be just "Back", nothing more. So how to do that?
This doesn't help
self.navigationItem.leftBarButtonItem.title = #"Back";
To create my on type barButtonItem(for example 104 with left arrow) and to set it to leftBarButtonItem is terrible decision.
Is there other way than to change the title of the rootViewController manually before pushing?
From Apple's doc:
backBarButtonItem
The bar button item to use when a back button is needed on the
navigation bar.
#property(nonatomic, retain) UIBarButtonItem *backBarButtonItem
Discussion
When this navigation item is immediately below the top item in the
stack, the navigation controller derives the back button for the
navigation bar from this navigation item. When this property is nil,
the navigation item uses the value in its title property to create an
appropriate back button. If you want to specify a custom image or
title for the back button, you can assign a custom bar button item
(with your custom title or image) to this property instead. When
configuring your bar button item, do not assign a custom view to it;
the navigation item ignores custom views in the back bar button
anyway.
So, you can create create your barButtonItem (e.g. – initWithTitle:style:target:action:) and assign it to that property.
In addition, if you want to have a custom image for UIBarButtonItem (left or right) I suggest you to create a category extension like the following:
//UIBarButtonItem+Extension.h
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image title:(NSString*)title target:(id)target action:(SEL)action;
//UIBarButtonItem+Extension.m
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image title:(NSString*)title target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.titleLabel.textAlignment = UITextAlignmentCenter;
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
return [barButtonItem autorelease];
}
and then use it as
UIBarButtonItem* backBarButtonItem = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:#"YoutImageName"] title:#"YourTitle" target:self action:#selector(doSomething:)];
I finally figured out all the wrinkles in this today, and it's simpler then above.
The child's back button text is based on values set in its parent. This is obvious behaviour, when you think about it: If a view controller can be reached from two parents, the back button's text should depend on which pushed it.
If the text is always the same:
Select the parent view controller's Navigation Item in the editor.
Put the text into the Back Button value.
And like that you're done. When this view controller is pushed aside by a new view controller, that new view controller will get this text as its title.
If the text is dynamic:
Select the parent view controller's Navigation Item in the editor.
Put some text into the Back Button value.
Set the title when it should change in the parent view controller: self.navigationItem.backBarButtonItem.title = dynamicText;
And, again, you're done.
To be clear, you can set this at any time in the parent view controller. It will only be shown when another view controller is pushed.
If you don't put the text in the Back Button in the designer, the process of instantiating the view controller won't create self.navigationItem.backBarButtonItem, and you can't set the title of a nil object. I believe this is where all of the confusion around this stems from.
Of course, you can create this at runtime, but if you're already doing most of your work in the storyboard/nib it's easier to let the decoder do it for you.
If you're more curious about this, I just wrote a blog post on the subject as well. It has some more details.
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] init];
backButtonItem.title = NSLocalizedString(#"Back", nil);
self.navigationItem.backBarButtonItem = backButtonItem;
It works for me:
[self.navigationItem.leftBarButtonItem setTitle:#"Back"];
(back arrow image and custom text)
I have a TabBar that I've created through IB, I chose "create new project" -> "Tab bar application". Is there a way for me to access one of the TabBarItems for customization through the code?
It seems to me that something like:
[[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:#"Button one"];
should set the title of that item to "Button one", but it doesen't. The title itself is not a problem (I can set that through IB aswell), however adding an Icon seems to be.
So to sum up, what I really want to know is: Is there a way to add an Icon to a TabBarItem created through IB?
SOLUTION:
Adding in viewDidLoad in the first view, being loaded automatically upon starting the app:
UITabBarController *tb = [self tabBarController];
[[tb.tabBar.items objectAtIndex:1] setTitle:#"Title"];
Let me set the title of the second button (objectAtIndex: 1). I was also able to set the image the same way, which also worked for buttons one (objectAtIndex: 0) and three (objectAtIndex: 2).
Add this to your viewDidLoad: method of one of the tabBar viewControllers and it should work:
- (void)viewDidLoad
{
[super viewDidLoad];
//Get the tabBarItem
UITabBarItem *tbi = [self tabBarItem];
//Give it a lable
[tbi setTitle:#"Title A"];
//create a image from a file for the tabBar
UIImage *i = [UIImage imageNamed:#"NiceImage.png"];
//and put it on the tabBar
[tbi setImage:i];
}
You should be able to set the image and title properties on the TabBarItems:
UITabBarItem *item = (UITabBarItem *)[tabBarController.tabBar.items objectAtIndex:0];
item.image = [UIImage imageNamed:#"home.png"];
Don't forget that the UITabBar only uses the alpha values out of the image you set, so if you don't have an alpha channel in the image you may not see anything when you set an image on the tab bar item.
I've never created a tab bar through IB (always through code), however to set title and icon I use
controller.title = #"Controller";
controller.tabBarItem.image = [UIImage imageNamed:#"image.png"];
where controller is the UIViewController added to the viewControllers' array of the UITabBarController.