Basic Request: How to Get Text Input in Cocos2d Application - ios

I have what I thought would be a simple task: create two input fields on a scene and perform the usual input methods on them (touch to set input focus, allow text entry using on-screen keyboard, deselect field when "Done" key is pressed, re-establish input focus if the field is touched/selected again, etc.), basically the usual functionality that you see in most applications that allow user text input.
I have looked through most of the relevant forums for iOS development, especially StackOverflow, and I have only seen incomplete code snippets and vague references, some of which don't even exist anymore.
I thought that this would be a simple task, but everyone seems to think that vague directions are sufficient for someone who just simply wants a straight answer.
Can someone provide a complete description of what needs to be done to accomplish this task?
Thanks.
p.s.: After realizing that I didn't provide complete information, this is the environment and tools that I'm using:
Library Framework: Cocos2D v2
Development Studio: Xcode 5.1
Target Platforms: iOS on iPhone, iPad, and iPod touch devices (port to Android platforms in progress)
Minimum iOS required: 6.1

If you are using Cocos2d v3, use CCTextField for text input. For example:
CCTextField *enterName = [CCTextField textFieldWithSpriteFrame:[CCSpriteFrame frameWithImageNamed:#"textfield_background.png"]];
enterName.fontSize = 16.0f;
enterName.contentSize = CGSizeMake(100.0f, 50.0f);
enterName.preferredSize =CGSizeMake(100.0f, 50.0f);
enterName.positionType = CCPositionTypeNormalized;
enterName.position = ccp(0.5f, 0.5f);
[self addChild:enterName z:5];
If you are using Cocos2d v2, use UIKit components. For example:
CGSize size = [[CCDirector sharedDirector] winSize];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(size.width * 0.5,
size.height* 0.1,
100, 100)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[[[CCDirector sharedDirector] view]addSubview:textField];
As a note, Cocos2d v3 actually uses a UITextField in iOS and a NSTextField in Mac.
#ifdef __CC_PLATFORM_IOS
/** iOS: UITextField used by the CCTextField. */
#property (nonatomic,readonly) UITextField* textField;
#elif defined(__CC_PLATFORM_MAC)
/** Mac: NSTextField used by the CCTextField. */
#property (nonatomic,readonly) NSTextField* textField;
#endif

You can use basic UIView and add it over the cocos2d layers, using this code:
[[[CCDirector sharedDirector] view] addSubview:yourCustomView];

Related

Disable image recognition for UIImageView on iOS 14 with VoiceOver

I have a UIImageView as the background for a view. User interaction is disabled. isAccessibilityElement is set to NO. This is verified by using debug view hierarchy when the app is running on device.
And yet when I tap on the view that has that as the background is describes all the controls on it and then describes the image using automatic image recognition. Everything I've found online says this is a new iOS 14 feature and that it's great, but says nothing about how I can turn it off.
I even tried setting my own description string to at least try to override the image recognition to no avail. So - IS there a way to turn it off for a specific UIImageView(or even for the app overall) and if so how?
*** update ****
So I've confirmed this is specific to iOS 14. I have the following code in viewDidLoad:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"blueSky.jpg"]];
imageView.isAccessibilityElement = YES;
imageView.accessibilityLabel = #"I am not a description";
CGPoint origin = imageView.frame.origin;
origin.y = 50;
origin.x = 50;
CGRect imageFrame = imageView.frame;
imageFrame.origin = origin;
imageView.frame = imageFrame;
self.view.isAccessibilityElement = NO;
[self.view addSubview:imageView];
Where blueSky is, well, an image of blue sky. It's not using the asset catalogue. When I tap on the image with VoiceOver enabled in iOS 13 it reads "I am not a description". When I tap on it on an iOS 14.1 device it reads "I am not a description", then pauses for half a second, says "image", then proceeds to describe it "blue sky, cloudy". It's that last part that I cannot for the life of me figure out how to eliminate!
So I found the answer:
imageView.accessibilityTraits = UIAccessibilityTraitNone;
This tells the system to not consider it an image and so it doesn't try to recognize it.

Vuforia IOS cylinder example - Add button

I have successfully built the sample application from Vuforia. I am interested in taking the Cylinder Tracking and replacing the soccer ball that rotates around the soda can with a button that does not rotate but sits at a certain position in relation to the can (the center of the tracking image).
I am new to iOS development and am not sure quite how to do this. Ideally this would be done programmatically.
I was able to add a button to the CylinderTargetsViewController.mm using the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:#"Gossamer Rules" forState:UIControlStateNormal];
[button sizeToFit];
button.center = CGPointMake(320/2, 60);
[button addTarget:self action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonPressed:(UIButton *)button {
UIApplication *mySafari = [UIApplication sharedApplication];
NSURL *myURL = [[NSURL alloc]initWithString:#"http://www.example.com"];
[mySafari openURL:myURL];
}
However this adds a button to the screen whether the target is found or not and it has no spatial relationship to the target.
I tried moving this code into CylinderTargetsEAGLView.mm but got an error on line [self.view addSubview:button]; saying view was not a property of self.
I realize this is probably not the right approach. I'm thinking the button needs to be rendered as an openGl object or something so that it is 3 dimensional and can be seen from an angle when moving the target.
Has anyone done this or have any clue how it may be done?
I have decided to use the Vuforia Unity Package as there is a much steeper learning curve trying to learn pure OpenGL programming. The Unity route has much larger community support, and more tutorials / information are available.

Objective-C category for styling

What is the best way to create re-usable styling for UI objects for iOS app?
E.g. I want to use the same style for each UITextField - top/bottom line and thinking about creating a category to provide the styling and simply apply it in View Controller.
Is there any better solution to do this?
UITextField+TextFieldStyler.m
- (void)addTopBorderWithColor:(UIColor *)color
{
CGRect topBorderFrame = CGRectMake(0, 0, self.bounds.size.width, 1.0);
UIView *topBorder = [[UIView alloc] initWithFrame:topBorderFrame];
topBorder.backgroundColor = color;
[self addSubview:topBorder];
}
PXRLoginViewController.m
[self.nameFIeld addTopBorderWithColor:[UIColor whiteColor]];
[self.passwordField addTopBorderWithColor:[UIColor whiteColor]];
I believe that the approach you are using is completely fine. It's also used by Apple itself for the same use cases, looking at the doc
Note: Cocoa and Cocoa Touch include a variety of categories for some
of the primary framework classes.
The string-drawing functionality
mentioned in the introduction to this chapter is in fact already
provided for NSString by the NSStringDrawing category for OS X, which
includes the drawAtPoint:withAttributes: and
drawInRect:withAttributes: methods. For iOS, the UIStringDrawing
category includes methods such as drawAtPoint:withFont: and
drawInRect:withFont:.
As you can see, they use the same pattern for extending the drawing behaviour of the NSString.

magnifying glass for UITextField/UITextView -- what is it? [duplicate]

I'm using UITextField as a UISearchBar replacement and "stealing" the magnifying glass icon from the original UISearchBar with this crazy code:
UISearchBar *originalSearchBar = [[UISearchBar alloc] init];
for (UIView *searchBarSubview in [originalSearchBar subviews]) {
if([searchBarSubview isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)searchBarSubview;
[_textField setLeftView:[textField leftView]];
[_textField setLeftViewMode:UITextFieldViewModeAlways];
}
}
As you've probably guessed, I don't want to use my own bitmap.
Isn't there an easier accessible magnifying glass icon somewhere in Cocoa?
So, here's the code with the unicode character:
UILabel *magnifyingGlass = [[UILabel alloc] init];
[magnifyingGlass setText:[[NSString alloc] initWithUTF8String:"\xF0\x9F\x94\x8D"]];
[magnifyingGlass sizeToFit];
[textField setLeftView:magnifyingGlass];
[textField setLeftViewMode:UITextFieldViewModeAlways];
Edit: For plain look that fits iOS 7 style, add Unicode variation selector \U000025B6.
I don't know of any standard image for it in Cocoa (and there is no character for it in Unicode either).
An image for this is part of the Dock bundle, however:
/System/Library/CoreServices/Dock.app/Contents/Resources/ectl_search_magnify.png
As I couldn't get a plain unicode magnifying glass to appear, I decided to see what a standard UISearchBox uses. It turns out it's an image of a magnifying glass, which I've extracted and included below, though it's a very simple shape which would be trivial to reproduce perfectly.
Using OSX 10.7.5 I couldn't find the ectl_search_magnify.png but a simple
find /System -ipath '*magni*'
found:
/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MagnifyingGlassIcon.icns
/System/Library/PrivateFrameworks/ProKit.framework/Versions/A/Resources/LionPanels.bundle/Contents/Resources/Dark_NSSmallMagnifyingGlass.tiff
/System/Library/PrivateFrameworks/ProKit.framework/Versions/A/Resources/LionPanels.bundle/Contents/Resources/Night_NSSmallMagnifyingGlass.tiff
The latter two are probably what you want (assuming you need an image).

Make the iPhone Screen Dim

I have managed to ensure that the iPhone doesn't auto-lock using:
[[ UIApplication sharedApplication ] setIdleTimerDisabled: YES ];
But how do I make the screen dim after a certain amount of time?
Thanks...
EDIT:
Think I've found a solution myself:
Use this method in your view controller to dim by adding a black view with 50% alpha. Make sure to set userInteractionEnabled = NO to pass events to underlying views.
- (IBAction)dim:(id)sender {
UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
dimView.backgroundColor = [UIColor blackColor];
dimView.alpha = 0.5f;
dimView.userInteractionEnabled = NO;
[self.view addSubview:dimView];
}
or, could risk it (but Apple may reject on submission):
[(id)[UIApplication sharedApplication] setBacklightLevel:1.0f];
That's a private API and shouldn't be used though...
As of iOS 5 there is a public API in the UIScreen class. It has a brightness property that can be set. For those instances where you may want to go dimmer than the actual backlight allows, there is a wantsSoftwareDimming property that will automatically place a translucent layer that will give the appearance of being more dim than can be done in hardware. This is very similar to the method you came up with with the translucent UIView. It should be noted that using your solution or the software dimming API should not be used with many animations since you will pay a performance penalty with all the alpha blending.
See UIScreen Class Reference
in iOS 7 and later:
yourViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;

Resources