I am trying to use an external font in a project. Already configured the file "react-native-config", already added the font, already made the link, but when I set fontFamily, does not work. Does anyone know how to solve?
If you are using ios, you can add this code at the begining of didFinishLaunchingWithOptions in AppDelegate to check whether your font is installed or not
NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++)
{
NSString *fontFamily = [fontFamilies objectAtIndex:i];
NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
NSLog (#"%#: %#", fontFamily, fontNames);
}
If you are using Android, you have to add font weight at the end
fontFamily: "Montserrat-Regular"
or
fontFamily: "Montserrat-Bold"
Related
This code return Me nil in font.
I add the open sans ttf file to project folder.
What am I missing?
UIFont *font = [UIFont fontWithName:#"OpenSans-Bold" size:fontSize];
if (font)
{
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:font size:fontSize] range:NSMakeRange(location, length)];
label.attributedText = str;
}
First of all are your .ttf files registered in your .plist file ?
Second are your fonts added to "Copy Bundle Resources" ? (Under Target->Build Phases)
Next try this code to list all usable fonts and their names. This way you can make sure you use the right identifier for your font.
for (NSString* fontFamily in [UIFont familyNames]) {
NSArray *fontNames = [UIFont fontNamesForFamilyName:fontFamily];
NSLog (#"%#: %#", fontFamily, fontNames);
}
Edit
Since this answer is a bit old I updated it with the corresponding answer for Swift 3.0:
_ = UIFont.familyNames.map {
let fontNames = UIFont.fontNames(forFamilyName: $0)
print("Font Family: \($0), Font Names: \(fontNames)\n")
}
Hi there i have image views in my application to change pictures in them. but their name like imageview1, imageview2, and so.. i create a string which like:
`("imageview%i", number)`
so my string is imageview1 for example. and i need to change
self."**mystring**".image = [uiimage ...]
I looked key-value coding but i couldn't get it exactly. i searched the forum and i can't get anything either. what could i do to resolve this. i think i must do an array with my uiimageviews inside of it. than compare their name with my string (i didn't know how can i get property names as nsstring). then return that image view. Please help.
If you want to do that in runtime, either hack your way using selectors or use tags on your UIImageViews. They are not really good programming concepts, but they work. Examples:
-(void)usingSelectors{
for(int i=0; i < 4; i++){
NSString* propName = [NSString stringWithFormat:#"imageview%d", i];
UIImageView* imageView = [self performSelector:NSSelectorFromString(propName)];
[imageView setImage:/*your image*/];
}
}
-(void)usingTags{
for(int i=0; i < 4; i++){
UIImageView* imageView = [self.view viewWithTag:i];
[imageView setImage:/*your image*/];
}
}
If you want to do that in compile time you'll have to use preprocessor macros, but I don't think that's your case.
I have an NSAttributedString in a UITextView and would like to handle the UIContentSizeCategoryDidChangeNotification when working with Dynamic Type and specifically the text styles. All the examples I've seen (IntroToTextKitDemo) address the case where the font is the same for the whole UI element. Does anyone know how to handle this properly so all the attributes update properly?
Note: I asked this on the developer forums when iOS 7 was under NDA. I'm posting it here because I found a solution and thought others might find it useful.
I found a solution. When handling the notification you need to walk the attributes and look for the text styles and update the font:
- (void)preferredContentSizeChanged:(NSNotification *)aNotification
{
UITextView *textView = <the text view holding your attributed text>
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
NSRange range = NSMakeRange(0, attributedString.length - 1);
// Walk the string's attributes
[attributedString enumerateAttributesInRange:range options:NSAttributedStringEnumerationReverse usingBlock:
^(NSDictionary *attributes, NSRange range, BOOL *stop) {
// Find the font descriptor which is based on the old font size change
NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
UIFont *font = mutableAttributes[#"NSFont"];
UIFontDescriptor *fontDescriptor = font.fontDescriptor;
// Get the text style and get a new font descriptor based on the style and update font size
id styleAttribute = [fontDescriptor objectForKey:UIFontDescriptorTextStyleAttribute];
UIFontDescriptor *newFontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:styleAttribute];
// Get the new font from the new font descriptor and update the font attribute over the range
UIFont *newFont = [UIFont fontWithDescriptor:newFontDescriptor size:0.0];
[attributedString addAttribute:NSFontAttributeName value:newFont range:range];
}];
textView.attributedText = attributedString;
}
I am using a textview in which I have passed an array of objects. In that array I have taken a list of messages that would be passed to the textview, but I have changed the size of textview font so. give any suggestion and source code which is apply in my application.
Below my code in that I have apply the UIFont but that is not use in font.
msgtxtView.text=selectedmsg;
[msgtxtView setFont:[UIFont fontWithName:#"Arial" size:10]]
selectedmsg is array of object in that message list that would be pass to textview's object but output font size and arialfont not work give any.
Use this :
msgtxtView.text=selectedmsg;
[msgtxtView setFont:[UIFont fontWithName:#"ArialMT" size:10]]
Look at his example how to add text from array and change size (You changing size correctly):
NSMutableArray *array = [NSMutableArray arrayWithObjects:#"message1", #"message2", #"message3", #"message4", #"message5", nil];
for (int i=0; i<[array count]; i++) {
msgtxtView.text = [NSString stringWithFormat:#"%#%#\n",msgtxtView.text,[array objectAtIndex:i]];
}
[msgtxtView setFont:[UIFont fontWithName:#"Arial" size:10]];
Don't forget to IBOutlet msgtxtView.
Result:
And for example using this: [UIFont fontWithName:#"Chalkduster" size:25]:
I have to show the Font Preview of a font. For that I just Copied the character set from the font and then I showed as the Preview. This is my coding.
NSString *fontName=#"aFontName";
CFStringRef CFFontName = (CFStringRef)fontName;
CTFontRef fontRef = CTFontCreateWithName(CFFontName, 0.0,NULL);
NSCharacterSet *characterset = (NSCharacterSet *) CTFontCopyCharacterSet (fontRef);
unichar idx;
NSString *str=[[NSString alloc]init];
for( idx = 0; idx < 65000; idx++ )
{
if ([characterset characterIsMember: idx])
{
if ( isprint(idx) ) {
NSString *str2 = [NSString stringWithFormat:#"%c",idx];
str=[str stringByAppendingString:str2];
}
else {
NSString *str1 = [NSString stringWithFormat:#"%C",idx];
str=[str stringByAppendingString:str1];
}
}
}
Then Finally in the TextView I showed the Strings from the str as Font Preview. But str has lot of unwanted characters(For the Hindi Language Font it is showing the English character also) . I think it is not the correct way for showing the Preview of a font. Is there is any other way for showing the font preview like FontBook application in mac?
FontBook is using a private routine to get a sample string from the font. If private routines are okay for your app, then you can simply define:
CFStringRef CTFontCopySampleString(CTFontRef font, int options);
and call like:
NSString* sample = (NSString *) CTFontCopySampleString(fontRef, 0);
and you'll get the same string as FontBook shows.