问题
I am new to ios, Just need to clear two doubts:
1.I am aware that to set custom colors we use following method:
[UIColor colorWithRed: 127 green:127 blue:127 alpha:1]
How do I find out from an Image what parameters for RGB are?(Its not an xcode question but I need to know).
2.How to add my own custom fonts in my project. I have added the file name in "fonts provided by application". But I am still not able to get the result in desired font. The file name is ExpertSans-ExtraBold.ttf .following is the code used:
setFont:[UIFont fontWithName:@"ExpertSans-ExtraBold" size:48.0f];
回答1:
For number 1, if it's not an Xcode question, you have an app located in /Applications/Utilities
on your mac, which lets you get the color of anything showing on your screen. (sorry, I don't know the name of the app in english, but it may have "color" in it). Then set the UIColor
as you did : [UIColor colorWithRed: 100.0/255.0f green:200.0/255.0f blue:150.0/255.0f alpha:1.0];
For number 2, the name of the font is not always the name of the file. Once you added your font to the project, use this to log a list of all the font names available in your app :
for (NSString* family in [UIFont familyNames])
{
NSLog(@"%@", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(@" %@", name);
}
}
Put this code in the AppDelegate
in application:didFinishLaunchingWithOptions:
Once you found your font name, use myLabel.font = [UIFont fontWithName:@"My-Font-Name" size:10];
to set the font.
回答2:
for set color
[UIColor colorWithRed: 127.0/255.0f green:127.0/255.0f blue:127.0/255.0f alpha:1.0];
for set custom font
- How to use custom font in iOS Apps?
- iPhone Development: how to use custom fonts?
回答3:
Try to use this one
[UIColor colorWithRed: 127/255.0f green:127/255.0f blue:127/255.0f alpha:1];
You are right but i think you are not getting proper font name. So do this to get proper name of font. See in this pic and set this name to your font.

set your font like this
setFont:[UIFont fontWithName:@"BankGothic Md BT" size:48.0f];
回答4:
you can not get the whole image color but you can get the color of pixel
iPhone Objective C: How to get a pixel's color of the touched point on an UIImageView? How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?
For font you have to give font name not file name
lblName.font=[UIFont fontWithName:@"Myriad Pro" size:15]; // where my file name is MyriadPro.ttf
回答5:
Please check label.font = [UIFont fontWithName:@"Font-Name" size:10];
and put NSLog(@"%@",label.font);
If you find null then i think there is problem in the name you are giving.
Please also ensure when you are adding the font's file .ttf
in your project it is also included in your target application. When you copy .ttf
from other location to resource folder then it asks to copy to destination, and on that place you have to add it to the target application also by using check mark. If you suspect you haven't included to your target application. Please remove the files and add the again.
Hope this helps.
来源:https://stackoverflow.com/questions/16581191/set-own-custom-colors-and-fonts-in-uicolor