Setting title on NSMenuItem, without effect - objective-c

I'm getting an NSMenuItem from the Main Menu, with the code here : Getting NSMenuItem of NSMenu tree by title
However, something strange happens :
An NSMenuItem connected with an action : When using the sender
property (NSMenuItem) and setting the title, it works.
BUT : When getting the item with the function above and set the title,
the NSMenuItem's title does change, but the change is NOT
reflected in the menu it belongs to.
What am I doing wrong? (I'm sure this one is really stupid... )
NSMenuItem* mi = [[core mainMenu] getItemWithPath:#"View" tag:PP_MENU_TAG_STATUSBAR];
[mi setTitle:#"newTitle"];
NSLog(#"mi : %#",[mi title]);
// mi changes, but no changes take effect in the mainMenu

I would forget the
Getting NSMenuItem of NSMenu tree by title code and just connect each menu in IB.
Then use the setTitle on it when needed
UPDATE*
(see comments)
It took me awhile to figure out why even my test one was not working!!. I had put in a attributed title within IB.
So when I later used setTitle. The property was being set but the actual displayed menu was overridden by the attributed title.
Removing the attributed title from IB. fixed this. And setTitle works as expected.
Also I have never used attributed title before. And I just pasted in some formatted coloured text in the IB attributed title. And the menu item was the same in colour and font.
Which I have always wanted to be able to do but thought was not possible.
And doing it programmatically is easy.
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:#"newTestMenu"];
[string addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(0,string.length)];
[_testMenu setAttributedTitle:string];

Related

Click on UILabel and perform action

I have a simple problem, I have a string like "#my#name#is#umesh#verma" and assign to a UITableview cell label,
cell.detaillabel.text = #"#my#name#is#umesh#verma";
My Problem is how to get each word name when I click on single item.
If I click on #umesh then I get "umesh" word..
More better solution is add custom label which supports touches. For example TTTAttributedLabel supports touches on links.
Main task is get notification when user touch a word and to identify the word.
You can add URLs (with special format) to any word and subscribe to a notification when user click it (as delegate to the label). For example you can create this URL for "word":
touchscheme://word
I haven't checked about how to perform an action when clicking on a UILabel. However, I experienced that with UITextView. You can use "NSLinkAttributeName" to do that.
Basically, from your original string, try to find the range of string that you need to trigger actions. Then add a link value.
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:yourString];
[attributedString addAttribute:NSLinkAttributeName value:url range:range];
[textView setAttributedText:attributedString];
Set delegate to your textView and handle the following method
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
// You can retrive your string here or perform an action
return YES;
}
Hope this would be helpful for you.

Binding image to NSPopupbutton from NSArrayController

This question is an extension to link
(The question in the link mainly targets, binding NSPopupbutton to a NSArrayController)
I have a Person class having properties NSString *name and NSImage *avatar
I have to show all the names of persons in Popup button as seen in the below image.
But now, as requirement has changed, I need to show avatar of person also.
How do I use Cocoa bindings to bind person's avatar to NSPopup button so that it looks like the one in above image for michael(last menu option)
Note: Michael has been temporarily added for demonstration using following code:
person.title = #"Michael";
person.image = [NSImage imageNamed:#"avatar.png"];
[_popupButton.menu addItem:person];
There are two ways you can achieve that:-
First take the cell-based tableview inside that two column one for image cell and second for text cell. Populate the table through bindings and then add your tableview inside your popup button in the below way:-
NSMenu *theMenu = [[NSMenu alloc] initWithTitle:#""];
NSMenuItem *Item = [[NSMenuItem alloc] initWithTitle:#"NameList" action:NULL keyEquivalent:#""];
[Item setView:self.tableVw];
[theMenu addItem:Item];
[self.popUptn setMenu:theMenu];
Second follow the below simple steps:-
Assuming your array contains the name elements.
1) Select your arrayContoller bind to FileOwner's and ModelKeyPath->array.
2) Select PopUpButton inside binding Inspector->Selected Object bind to FileOwner's and ModelKeyPath->yourString. This will select the required name accordingly.
3) Select PopUpButton inside binding Inspector->Content Valuesand ModelKeyPath->array
4) Now for setting the image inside popupButton refer below code:-
NSMenuItem *menuItm=[self.popUpBtn itemWithTitle:#"Michael"];
[menuItm setImage:[NSImage imageNamed:#"dot.gif"]];
Edit:-
1) In the first method your text and image both are populating through binding just you need to add that tableview inside your popupbutton.
2) In the second method your popupbutton will display the image for last name but programmatically. And also, if required to display the images for all names then use for loop to set images inside menu item.

iOS7 custom interactive transition, hidden back button reappears as "..." when cancelled

I have a custom interactive transition which requires me to hide the standard back button. Basically, the transition looks like a push from left-to-right rather than the standard right-to-left push we're all familiar with. That's why my back button is on the right side instead.
As you can see from two screenshots I took before and after cancelling pop transition activated by a UIScreenEdgePanGestureRecognizer, once the transition is cancelled there is a "..." where the back button would be.
I'm currently using
self.navigationItem.hidesBackButton = YES;
and I've tried putting it in awakeFromNib, viewDidLoad, viewDidAppear, viewWillAppear methods all without fixing the problem.
So using the power of Reveal.app I investigated the view hierarchy before and after and saw this:
What you see highlighted in each part of the image is what appears to be changing in the area of the nav bar that contains the hidden back button. Before it's a UINavigationButton and then it becomes a UINavigationButtonItem with a UILabel, which must be what contains the "..." and remains like this.
Any help would be much appreciated. I hope this is detailed enough to give a good picture of the issue.
Try creating an empty backbutton first (in the parent viewcontroller before the vc is pushed) - maybe that will prevent the "..." UILabel from being created.
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:#""
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
Another idea: Just set the parent vc title to an empty string.
self.title = #"";

ios 7 UIBarButtonItem UIAppearance not setting font

I'm using this piece of code to set the default font (Custom) for all my UIBarButtonItems:
NSDictionary *attributesBarButtonItem = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"ProximaNova-Light" size:18.0], NSFontAttributeName, nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributesBarButtonItem forState:UIControlStateNormal];
NSLog(#"%#", [[UIBarButtonItem appearance] titleTextAttributesForState:UIControlStateNormal]);
However, it seems to be ignored as the font does not change, and NSLog returns (null). It's a bit confusing because its pretty much the same code I'm using to set the default font for all my navigation bars and it works fine for them.
The piece of code is placed in AppDelegate´s didFinishLaunchingWithOptions but I've also test it in other viewControllers (viewDidLoad) with exact same result.
Other strange behaviour I've noticed:
I've got a tab bar controller, and when I load any viewController with bar button items it doesn't work, but if I push another viewController it works (The font is changed to the selected one), and it keeps working even if that viewController is popped out, although it will stop working if another tab is pushed.
Any help to try to set a default font for the UIBarButtonItems would be appreciated. Thanks!
Is this your custom font ?
There could be few problems:
is the font in TTF format ?
if you click on the font in xcode is Target membership in right panel checked ?
did you add the font to project plist file ?
Also you should use UITextAttributeFont in the dictionary:
[UIBarButtonItem appearance] setTitleTextAttributes:#{UITextAttributeFont:[UIFont fontWithName:#"ProximaNova-Light" size:18.0]} [forState:forState:UIControlStateNormal];
I had a similar issue arise because I was creating the leftBarButtonItem before I set the appearance attributes. Swapping the order such that appearance was set first fixed the problem.

setting an accessibilityLabel on a UIImageView contained in UITableView header

I have a UITableView that I build in loadView. One of the things I do in loadView is create a UIView to act as the table header and stuff a UIImageView into it. The image view contains an image that is a stylized title, so I want to add an accessibility label for VoiceOver users. However, I can't get VoiceOver to "focus" on the image in order to read the label, and the Accessibility Inspector doesn't respond to clicking on the image in the simulator. My (abbreviated) code follows:
... in -loadView ...
// Make header view
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(...)];
UIImageView *titleImageView = [[UIImageView alloc] initWithImage:[self titleImage]];
titleImageView.accessibilityLabel = [self accessibilityLabelForTitleImage];
[headerView addSubview:titleImageView];
// Make table view
self.tableView = [[UITableView alloc] initWithFrame:CGRect(...) style:UITableViewStylePlain];
self.tableView.tableHeaderView = headerView;
... code continues ...
I've stepped through in gdb and accessibilityLabelForTitleImage returns a string. po [titleImageView accessibilityLabel] prints out the correct string, but I'm still unable to focus on the image view. Note that the views themselves appear and respond as appropriate.
Am I missing something? Is there a way to force VoiceOver to acknowledge an image view?
In Voice-Over , in order to make an element accessible :-
you have to set setIsAccessibilityElement property as true which i don't find in your code.
The other important point is that to make child elements (subviews) to be accessible , you have to seperately make them accessible while the parent should not be accessible(you have to specify this also).
Implement the UIAccessibilityContainer Protocol in your custom - cell.
It will be a big story if i go on .Please refer this Accessibility voice over by apple.
Hope this helps.
I used KIF for testing my IOS app. In my tableview, I assigned value to tableview.accesssibilityIdentifier instead of tableview.accessibilityLabel. It worked for me. Wanna give it a try?
Voice-Over sometimes can get nasty and just by setting isAccessibilityElement might not work.
In this case try setting accessibilityElements on the parent view and include the child views in the array, like this:
parentView.accessibilityElements = [childView1, childView1, childView1]
Doing it also ensures that the accessibility items are being read in the order you want.