NSTokenField: different colors for tokens - objective-c

I have created an NSTokenField in my xib. I'd like to display tokens with different colors inside that tokenField. Meaning: some tokens will be blue, the rest will be red (according to their content). Is that possible?
The following code doesn't work for me. I hope someone can help me out:
- (id)tokenField:(NSTokenField *)tokenField representedObjectForEditingString:(NSString *)editingString
{
id returnRepresentedObject = nil;
NSTokenFieldCell *tf = [[NSTokenFieldCell alloc] init];
tf.stringValue = editingString;
tf.backgroundColor = [NSColor redColor];
returnRepresentedObject = tf;
return returnRepresentedObject;
}
Result: all tokens remain blue... :-(
Any help will be highly appreciated!

Its possible by using private APIs. Subclass NSTokenAttachmentCell (Private) and NSTokenFieldCell.
Sample project
Use BWTokenAttachmentCell and BWTokenFieldCell class and NSTokenAttachmentCell class dump from BWToolkit. Modify initialize method of BWTokenAttachmentCell.
[sample project
NOTE:
Use this method if you are not targeting for Mac App Store.

You'll probably have to role your own. There is a wwdc video from 2010 about advanced Cocoa Text handling. The NSTokenField Uses NSTextAttachments to render the tokens.

Related

Find images not used or override +imageNamed: to log images not found

I am developing an iOS application with many images. because i have a designer on my side every moment i am using many images that i don't need anymore.
So at the moment, to clean up this mess i was thinking: If i use a category of UIImage can i override the +imageNamed: to log the name of the image and maybe the method that called it?
something like this:
#implementation UIImage (FindImage)
+ (UIImage *)imageNamed:(NSString *)name {
UIImage *img = [self imageNamed:name];
if (img) {
return img;
} else {
NSLog(#"\n\nImage not found: %#\n\n", name);
return nil;
}
}
#end
I want to override imageNamed because:
I am using it in many different classes
I believe that when a xib load an image it call this method
on resume, my question is: Can i log all images that could not be found on bundle?
EDIT
Refining my question:
i see my question is a little bit complicated so i`m going to try elucidate it.
I want to find all images that my app tries to use, but in my project this images are not found.
I can remove all images, i can create a subclass, i can create categories, i really don't care. Maybe there is a flag on Xcode to complain about missing images...
all i want is to find all places where i`m using an image an this image is not found on bundle.
You could use https://github.com/steipete/Aspects to inject a logging instruction to the end of +imageNamed:

Setting the background picture position

Is there any Mac App Store compliant way of setting the desktop wallpaper position to "Stretch to Fill Screen"?
This code sample from Apple provides a good overview of the problem and how to properly code for it in Cocoa. The salient code snippet appears to be the following:
- (void)updateScreenOptions:(NSScreen*)screen
{
if (screen)
{
NSDictionary *screenOptions = [[NSWorkspace sharedWorkspace] desktopImageOptionsForScreen:curScreen];
// the value is an NSNumber containing an NSImageScaling (scaling factor)
NSNumber *scalingFactor = [screenOptions objectForKey:NSWorkspaceDesktopImageScalingKey];
[scalingPopup selectItemAtIndex:[scalingFactor integerValue]];
// the value is an NSNumber containing a BOOL (allow clipping)
NSNumber *allowClipping = [screenOptions objectForKey:NSWorkspaceDesktopImageAllowClippingKey];
[[clippingCheckbox cell] setState:[allowClipping boolValue]];
// the value is an NSColor (fill color)
NSColor *fillColorValue = [screenOptions objectForKey:NSWorkspaceDesktopImageFillColorKey];
if (fillColorValue)
[fillColor setColor:fillColorValue];
}
}
As mentioned in some of the answer errata, the correct method calls here to resolve your question appear to be [screenOptions setObject:scalingFactor forKey:NSWorkspaceDesktopImageScalingKey] and [[NSWorkspace sharedWorkspace] setDesktopImageURL:imageURL forScreen:curScreen options:screenOptions error:&error], which should provide the desired functionality.
I freely admit this was a collaborative effort, but it seems we've blundered through to a correct solution.
(Additional information on the NSImageScaling enumeration and NSWorkspace, which contains the correct scaling keys.)
Unfortunately there is no Mac App Store compliant way of setting the desktop image options. They only methods available with regards to desktop images are:
– desktopImageURLForScreen:
– setDesktopImageURL:forScreen:options:error:
– desktopImageOptionsForScreen:
You may be able to do it by executing an AppleScript, though I do not think Apple will like that.
Hope this helps!
EDIT:
[screenOptions setObject:scalingFactor forKey:NSWorkspaceDesktopImageScalingKey];
[[NSWorkspace sharedWorkspace] setDesktopImageURL:imageURL forScreen:curScreen options:screenOptions error:&error];

i looking for how make a refresh of tableView in pulling down?

if anyone can help me for make that (pull down to refresh):
source: http://cdn.iphone4jailbreak.org/wp-content/uploads/pull-to-refresh-jailbreak-app-iphone-4.jpg
There are several resources to do that:
EGOTableViewPullRefresh - An open source project hosted on
GitHub.
This tutorial from CocoaNetics.
Included in the three20 library
You can take all needful source codes here: EGOTableViewPullRefresh
You can subclass TTTableViewController and then delegate it to TTTableViewDragRefreshDelegate.
- (id<TTTableViewDelegate>) createDelegate {
TTTableViewDragRefreshDelegate *delegate = [[TTTableViewDragRefreshDelegate alloc] initWithController:self];
return [delegate autorelease];
}

UILabel text as html text

I am stack with a small issue.
I need to use a sentence which will have first two words bold and last two words italic.
like I am using an Objective C Developer.
How to do that. Is this possible in Objective C?
Regards,
For iOS7 you can use this:
NSString * htmlString = #"<html><body> Some html string </body></html>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UILabel * myLabel = [UILabel alloc] init];
myLabel.attributedText = attrStr;
Apple recommends that for small amounts of styled text, you should use a web view, and display the text marked up in HTML and formatted with CSS, etc.
Personally I've never taken that advice, as I would consider a UI full of web views to be a bit over kill.
There is the Core Text framework, if you want a little more control over your text and want to use attributed strings.
It isn't a one-liner though. Using core text requires quite a lot of code.
I have written a core text view that will display tappable URL links inline with text, but I have not given it arbitrary formatting support. If you're interested in showing links within text, then check it out: https://github.com/jasarien/CoreTextHyperlinkView
You may be interested in Oliver Drobnik's rich text view, which is based on Core Text too. As far as I know, you can feed it HTML and it'll produce a native view containing your formatted text. Very useful. It can be found here: https://github.com/Cocoanetics/DTCoreText
I've written a very small class called THMultiPartLabel to help me accomplish this sort of thing - you can find it on GitHub here. It's based heavily on Jason's answer to a similar question here. Using this class, you'd implement your example like so:
UIFont *normal = [UIFont systemFontOfSize:20];
UIFont *bold = [UIFont boldSystemFontOfSize:20];
UIFont *italic = [UIFont italicSystemFontOfSize:20];
NSArray *fonts = [NSArray arrayWithObjects: normal, bold, normal, italic, normal nil];
THMultiPartLabel *mpLabel = [[THMultiPartLabel alloc] initWithOffsetX:0 Y:0 defaultFonts:fonts];
[mpLabel updateText:#"I ", #"am using ", #"an ", #"Objective C ", "Developer", nil];
You should have a look at Core Text.
Here are some useful resources:
Befriending Core Text on Cocoanetics.com
Core Text Reference Collection - official Apple documentation
SimpleTextInput - Example app by Apple
CoreTextPageViewer - Example app by Apple
You can't do both with the standard UILabel implementation. But you can do one or the other.
myLabel.font = [UIFont fontWithName:#"TrebuchetMS-Bold" size:18];
Your best option is probably to replace the UILabel with a UIWebView and use HTML to do the formatting.
[EDIT]
If you have lots of them and think there is too much of a performance hit, you can (from iOS 3.2 onwards) also consider NSAttributedString, although that will be a lot more coding.
Since iOS 6 you can use NSAttributedString with an UILabel, a NSAttributedString takes a NSString as an argument in one of its contructors, after that you can specify which ranges of this string has a particular text style.
You can find a good example on how to do it on iOS 6 here: http://weblog.invasivecode.com/post/31931916393/introduction-to-nsattributedstring-for-ios-6
If you what something backwards compatible, I'm strongly recommend the OHAttributedLabel which can be found here: https://github.com/AliSoftware/OHAttributedLabel
Note: OHAttributedLabel also allows the usage of html markup to style your label text.

Get list of installed apps on iPhone

Is there a way (some API) to get the list of installed apps on an iPhone device.
While searching for similar questions, I found some thing related to url registration, but I think there must be some API to do this, as I don't want to do any thing with the app, I just want the list.
No, apps are sandboxed and Apple-accepted APIs do not include anything that would let you do that.
You can, however, test whether a certain app is installed:
if the app is known to handle URLs of a certain type
by using [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"thisapp://foo"]
You can get a list of apps and URL schemes from here.
For jailbroken devices you can use next snipped of code:
-(void)appInstalledList
{
static NSString* const path = #"/private/var/mobile/Library/Caches/com.apple.mobile.installation.plist";
NSDictionary *cacheDict = nil;
BOOL isDir = NO;
if ([[NSFileManager defaultManager] fileExistsAtPath: path isDirectory: &isDir] && !isDir)
{
cacheDict = [NSDictionary dictionaryWithContentsOfFile: path];
NSDictionary *system = [cacheDict objectForKey: #"System"]; // First check all system (jailbroken) apps
for (NSString *key in system)
{
NSLog(#"%#",key);
}
NSDictionary *user = [cacheDict objectForKey: #"User"]; // Then all the user (App Store /var/mobile/Applications) apps
for (NSString *key in user)
{
NSLog(#"%#",key);
}
return;
}
NSLog(#"can not find installed app plist");
}
for non jailbroken device, we can use third party framework which is called "ihaspp", also its free and apple accepted. Also they given good documentation how to integrate and how to use. May be this would be helpful to you. Good luck!!
https://github.com/danielamitay/iHasApp
You could do this by using the following:
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
SEL selector = NSSelectorFromString(#"defaultWorkspace");
NSObject* workspace = [LSApplicationWorkspace_class performSelector:selector];
SEL selectorALL = NSSelectorFromString(#"allApplications");
NSMutableArray *Allapps = [workspace performSelector:selectorALL];
NSLog(#"apps: %#", Allapps);
And then by accessing each element and splitting it you can get your app name, and even the Bundle Identifier, too.
Well, not sure if this was available back when the last answer was given or not (Prior to iOS 6)
Also this one is time intensive, yet simple:
Go into settings > Gen. >usage. The first category under usage at least right now is Storage.
It will show a partial list of apps. At the bottom of this partial list is a button that says "show all apps".
Tap that and you'll have to go through screen by screen, and take screenshots (Quick lock button and home button takes a screenshot).
I'm doing this now and I have hundreds of apps on my iPhone. So it's going to take me a while. But at least at the end of the process I'll have Images of all my apps.