new to Xcode. I have a static library creating a view, but I want to allow a new project to set the view frame in a configuration file that can change the frame size value in the static library. Some kind of global #define variable.
How do I do this in Xcode? I've looked into pbxuser files, pch, xconfigs, and plists but I'm totally lost as to where exactly I'm supposed to set this up.
You are supposed to pass the proper configuration (whether it will be view frame, color or any other parameters) when you're initializing class instance defined in the static library.
Don't overcomplicate things. You don't need any configuration file for that.
Lets say your config.plist file is under you project folder and added to your project :
NSString *configPath = [[NSBundle mainBundle] pathForResource:#"config" ofType:#"plist"];
NSDictionary *config = [NSDictionary dictionaryWithContentsOfFile:configPath];
float xStartPoint = [[config objectForKey:#"xStartPoint"] floatValue];
float yStartPoint = [[config objectForKey:#"yStartPoint"] floatValue];
float objectWidth = [[config objectForKey:#"objectWidth"] floatValue];
float objectHeight = [[config objectForKey:#"objectHeight"] floatValue];
UIView *exampleView = [[UIView alloc] initWithFrame:CGRectMake(xStartPoint, yStartPoint, objectWidth, objectHeight)];
[self.view addSubview:exampleView];
Example of the Plist file :
The .plist code
Create a plist and read the value on load. This would allow you to externally modify the plist file allowing for easy changes outside of Xcode (if that's his wish).
iPhone read/write .plist file
Related
I am Developing an app which supports two languages i.e English and Arabic. I have done mostly all tasks i.e localisation within app + handle UI LTR (english language) , RTL (Arabic language).
Now I want to see preview for a UI in RTL in storyboard. By default it is LTR.
What I have done till now:
+(void)initialize {
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:#"AppleLanguages"];
NSString *current = [languages objectAtIndex:0];
[self setLanguage:current];
}
+(void)setLanguage:(NSString *)l {
NSLog(#"\n\n\t ***** Hint Lang. selected by user: %# ****\n\n", l);
NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:#"lproj" ];
bundle = [NSBundle bundleWithPath:path];
}
+(NSString *)get:(NSString *)key alter:(NSString *)alternate {
return [bundle localizedStringForKey:key value:alternate table:nil];;
}
For handle UI LTR OR RTL. For example when user selects english language I set UI as
[UIView appearance].semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
and when user selects Arabic language then I set UI as
[UIView appearance].semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
But I have to check every and run app iPhone UI screen whether it is fine or not for both LTR as well as RTL.
Is there any way by which we can see preview UI as RTL in storyboard (without to run in iPhone/simulator). Any suggestion will be Great!! Thanks in advance!!
I found a useful way to quickly preview the RTL layout while editing a storyboard/xib, but only for one view (like a UIView parent container). This is not a good approach, but can save you some time and does not need to run the app.
While on the editor, you can select any View and in the inspector change the Semantic property to Force Right to Left. This will preview your layout in RTL for the selected view.
This is just a quick way to test a single container or control, the major drawback is that to preview the whole layout in RTL (including child views) you would need to go one by one and change this property.
I'm not sure if I'm doing this wrong or if there's something broken with the "atlasWithDictionary" method.
This is how I used it:
NSArray* imageNames = #[ #"image1", #"image2", #"image3", #"image4"];
NSMutableDictionary* tempDict = [NSMutableDictionary dictionary];
for (int i = 0; i < imageNames.count; i++) {
UIImage* texture = [UIImage imageNamed:imageNames[i]];
[tempDict setObject:texture forKey:imageNames[i]];
}
SKTextureAtlas* atlasFullOfTextures = [SKTextureAtlas atlasWithDictionary:tempDict];
and then later in my code, whenever I would do
SKTexture* tex = [atlasFullOfTextures textureNamed:#"image1"];
I just get a nil object. I did a bit of troubleshooting and found that
NSArray* arrayOfNames = [atlasFullOfTextures textureNames];
returns an empty array.
Also, I know that the images are loading fine as I temporarily made the dictionary public and successfully made SKTextures from the UIImage objects.
Does anyone have any idea what's happening?
From the documentation:
The keys in the dictionary represent the names of the individual
textures. The associated object for each key can be:
An NSString object that contains a file system path to a file that contains the texture
An NSURL object that contains a file system path to a file that contains the texture
A UIImage object
An NSImage object
You are not providing a path/url to the image, you just use image names with no way of telling where they might be. You will probably also have to specify the file extension as Sprite Kit probably won't try to guess it. If these are textures obtained or created at runtime, they will not be in the bundle so you have to specify what the path to each texture is as well (usually in the appdata or documents folder).
If these images are in the bundle, you probably have to specify the bundle directory. But there's little reason not to have the atlas be created by Xcode at compile time in this case and then using atlasNamed:.
I am an iOS newbie but I have some experience programming for Android. I recall that in Android you could define a set of custom colors in a colors.xml file that you could use in your code, and therefore, there was no need to define a new color programmatically each time it had to be applied somewhere.
My question is: does a similar mechanism exist in iOS? I have searched throughout the Internet for a way to create custom colors once in some file and use them programmatically in my iOS app but have found nothing.
Thanks in advance.
As vokilam suggested, it is always good to create a Category using UIColor and adding your own custom colors through class methods inside the category. It is quite handy and easy to have a track of colors inside a single class. Something like:
In Xcode, File -> New --> New ---> File... Cocoa Touch -> Objective-C Category -> Next -> Select UIColor in "Category on" list --> Give a name for the category --> Next --> Create
This gives you .h and .m file... In your .h file, add a custom color method like
+(UIColor *)customLightGreenColor;
And in the .m file, you can implement the method something like (You have different options to set your colors here):
+(UIColor *)customlightGreenColor
{
UIColor *lightGreenColor = [UIColor colorWithRed:0.5 green:0.87 blue:0.0 alpha:1.0];
return lightGreenColor;
}
Likewise, you can add any number of colors in your category and use it in your application. Hope this gives you a heads up...
You can extend UIColor class with category and define and implement colors there. Like [UIColor blackColor].
In fact, you can abstract from particular color names and use method names such as primaryColor, secondaryColor, defaultTextColor, alternativeTextColor, inverseTextColor and so on.
Follow these steps to create a resource file in which you'll keep your colors and their values:
1. Create and add a plist file in your project resources.
2. Set type of plist as array. Add different colors as array elements.
3. Set type of each color (array element) as dictionary.
4. Add a string item in each element to set color name of each item.
5. Add 3 float items as Red, Green, Blue for storing RGB color values.
You can use your color file wherever you want by following code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *colorsPlistPath = [documentsDirectory stringByAppendingPathComponent:#"Colors.plist"];
NSMutableArray *colors = [NSMutableArray arrayWithContentsOfFile:colorsPlistPath]];
//get color and their properties like:
NSDictionary *aColor = [colors objectAtIndex:0];
NSString *colorName = [aColor objectForKey:#"Color_Name"];
float redValue = [aColor floatForKey:#"Red"];
float greenValue = [aColor floatForKey:#"Green"];
float blueValue = [aColor floatForKey:#"Blue"];
UIColor *color = [UIColor colorWithRed:redValue/255.0f green:greenValue/255.0f blue:blueValue/255.0f alpha:1.0];
If you already know how to create and use plist files as resources, the whole process would be much easier for you.
I have a .xib file, with accompanying .strings files for different languages. The .xib file contains a label, and a UISegmentedControl.
When asking IB to localize the .xib file, I get the following .strings file:
"6.segmentTitles[0]" = "title1";
// ...More strings related to the segmented control...
"11.text" = "bla";
The 'bla' string belongs to the label.
Changing the 'bla` string is reflected in runtime, while changing the 'title1' string does not. Anyone knows why?
This question is not new, but as it is still without any answers I will add my solution as it may help others.
Generally, as it's mentioned above, it is an open bug that UISegmentedControl segment titles do not pick up localization strings.
- (void)viewDidLoad
{
...
// Locale will be picked automatically by NSBundle.
NSString *resourcePath =[[NSBundle mainBundle] pathForResource:#"MainStoryboard" ofType:#"strings"];
NSDictionary *resourceDict = [NSDictionary dictionaryWithContentsOfFile:resourcePath];
[self.segmentedControl setTitle:[resourceDict objectForKey:#"COo-BO-Ryl.segmentTitles[0]"] forSegmentAtIndex:0];
[self.segmentedControl setTitle:[resourceDict objectForKey:#"COo-BO-Ryl.segmentTitles[1]"] forSegmentAtIndex:1];
}
Where COo-BO-Ryl is the Object ID of segmentedControl.
Not very pretty, but does the job.
When I add this array of strings to the summaryText UILabel it crashes. Please let me know how to fix this.
NSMutableArray *arr = [[NSMutableArray alloc]init];
arr = [Singleton getArray];
NSString *str = [arr componentsJoinedByString:#"\n"];
summaryText.text = str;
This is what was brought up when i command clicked summaryText
#implementation TotalViewController
#synthesize tax,taxLabel,total,totalLabel,final,finalLabel,fiveLabel,threeLabel,twoLabel,five,three,two, points, pointsLabel,summaryText;
I had suggested initWithArray, but not clear why you don't replace entire above snippet with:
summaryText.text = [[Singleton getArray] componentsJoinedByString:#"\n"];
But as others pointed out, your crash isn't here, this just simplifies your code. The problem has to rest elsewhere, probably related to the definition/creation of summaryText. Hard to say without seeing crash log or more code.
Update:
You said that you created this control in Interface Builder. You might want to double check your "connections inspector" for that control and make sure your outlet is set up correctly. This sounds a lot like a control that hasn't been set up correctly in Interface Builder. Or you can look at you .h file in Xcode and it will tell you if it's successfully linked to a control in Interface Builder. You'll see a little "circle" to the left of the source code, a solid dot in the circle means that your outlet is hooked up correctly, and empty circle means that it's not (e.g. in this example below, contactName, contactAddress, and contactPhone are all linked up correctly, but myLabel is not):