Click on UILabel and perform action - objective-c

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.

Related

How can I bind to the Image property of a NSPopUpButtonCell?

I've created a NSTableView, and have set up all my bindings (via an NSArrayController). I want one of the columns to have a NSPopUpButtonCell, but, instead of showing text, I want it to show an image - so the user can change the image from the popup control.
My column is bound (using the SelectedObject binding) to the correct property, and the the NSPopUpButtonCell is bound (using the ContentValues binding) to my complete list of images (which is from another NSArrayController containing an array of image names - NSStrings).
I've created a NSValueTransformer to convert an image name (NSString) to an NSImage, and have tested this works by creating an addition column with an NSImageCell, and that displays the image correctly.
Is there a way to do this?
I'm hoping I can target the binding at the NSPopUpButtonCell's image property, rather than its title property.
Edit: I had the idea of trying to use an NSAttributedString, with an embedded image. But, the problem remains, in that I want the binding to use setAttributedTitle, rather than setTitle.
After a lot of trial and error, I've managed to solve this problem. I'm convinced there remains a more correct solution, but, for now, this'll do.
My solution:
Change the Pop Up Button Cell > Menu to a custom class
In the custom class, inherit from NSMenu:
#interface RSMenu : NSMenu
Override insertItemWithTitle, and customize to your own needs.
For example:
-(NSMenuItem *)insertItemWithTitle:(NSString *)aString
action:(SEL)aSelector
keyEquivalent:(NSString *)charCode
atIndex:(NSInteger)index {
NSImage *image = [NSImage imageNamed:aString];
NSMenuItem* menuItem = [super insertItemWithTitle:image ? #"" : aString
action:aSelector
keyEquivalent:charCode atIndex:index];
if ( nil != image ) {
[menuItem setImage:image];
}
return menuItem;
}
And here is the result:
Hope this helps someone else.

Xcode/ Printing dot

I am trying to print "." when I press on a button that shows "."
Basically I wanna grab "." on NSString format
So if I do
NSString *dec = [sender currentTitle];
it just crashes when I try to run.
In Cocoa, all controls send notifications that they have been operated, by using a target-action mechanism. The 'target' is any other object and the 'action' is any selector that that object responds to. Buttons are no different.
So you can, for example, define:
-(void)buttonClicked:(id)sender {
NSLog(#"Button was clicked!");
}
You'd hook that up to the button's target-action, by invoking -setTarget: and -setAction: accordingly. The target will be self, if you're doing this from inside the class that handles the action:
[button setTarget:self];
[button setAction:#selector(buttonClicked:)]
Now when the button is pressed, you'll get a NSLog() output in the console.
To update the value of a label instead of printing something with NSLog(), you can probably figure that out, but:
-(void)buttonClicked:(id)sender {
[label setText:#"."];
}
You should read Apple's documentation, which covers this stuff in great detail.
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW14
PS: stackoverflow is for all programming languages, so make sure to tag your questions with the relevant programming language.
Try this.
UIButton *resultButton = (UIButton *)sender;
NSString *dec = resultButton.currentTitle;
Try Something like this
NSString *strdec = [sender titleLabel].text;

Setting title on NSMenuItem, without effect

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];

find the location {x,y} of text in uilabel

I have a string coming from server which I am displaying on UILabel multiligne. It is within that string, I am identifying some particular substring. I want to place a button on that substring(button will be a subview of UILabel). For this I require substring coordinates. I went through this but I am not able to understand it. Suppose my complete string is abc, 567-324-6554, New York. I want 567-324-6554 to be displayed on button for which I need its coordinates.
How can I use above link to find coordinates of substring?
Thanks,
UILabel doesn't have any methods for doing this. You can do it with UITextView, because it implements the UITextInput protocol. You will want to set the text view's editable property to NO.
Something like this untested code should work:
- (CGRect)rectInTextView:(UITextView *)textView stringRange:(CFRange)stringRange {
UITextPosition *begin = [textView positionFromPosition:textView.beginningOfDocument offset:stringRange.location];
UITextPosition *end = [textView positionFromPosition:begin offset:stringRange.length];
UITextRange *textRange = [textView textRangeFromPosition:begin toPosition:end];
return [textView firstRectForRange:textRange];
}
That should return a CGRect (in the text view's coordinate system) that covers the substring specified by stringRange. You can set the button's frame to this rectangle, if you make the button a subview of the text view.
If the substring spans multiple lines, the rectangle will only cover the first line.

How do I programmatically change text attributes in a WebView?

I need to highlight certain segments of text within a WebView. Until now, I've been doing this by injecting javascript into the webview using [webView stringByEvaluatingJavaScriptFromString:jscript]. The javascript adds a tag to the text in which I change the background color to yellow. This usually works but sometimes I corrupt the html when the tag is inserted. I would rather use the - (void)changeAttributes:(id)sender method on WebView, which, as I understand, will apply the style in a way that won't break the underlying html. To do this I first call
[webView searchFor:aString direction:YES caseSensitive:NO wrap:YES];
To set the selection in the webview. Then I call
[webView changeAttributes:self];
When I send this message to my WebView instance, it invokes this method:
- (NSDictionary *)convertAttributes:(NSDictionary *)attributes {
NSMutableDictionary *newAttrs = [[NSMutableDictionary alloc] initWithDictionary:attributes];
NSColor *background = [NSColor yellowColor];
[newAttrs setValue:background forKey:NSBackgroundColorAttributeName];
return newAttrs;
}
Which should change the background color of the selected text to yellow, but it does not. Do standard Cocoa text attributes apply to a webview? Is this method intended to be used for this purpose, or should I go back to javascript?
These attributes are those for NSFonts. As described in this document, the sequence of events is usually this:
[text/webView changeAttributes:obj] is performed. Usually it's done by obj itself.
text/webView passes the font attributes to obj using [obj convertAttributes:...].
obj returns the attributes as it wants.
text/webView sets the font attributes accordingly.
Anyway I won't recommend this approach. I assume you're on OS X, not on iOS. Then, I would suggest you to get the DOM elements using WebFrame's DOMDocument and set the styles directly.