I have the following code for that creates multiple fonts.
UIFont* systemFont1 = [UIFont systemFontOfSize:12.0];
UIFont* systemFont2 = [UIFont boldSystemFontOfSize:12.0];
UIFont* systemFont3 = [UIFont italicSystemFontOfSize:12.0];
UIFont* customFont1 = [UIFont fontWithName:#"HelveticaNeue-Light" size:12.0];
UIFont* customFont2 = [UIFont fontWithName:#"HelveticaNeue-Regular" size:12.0];
UIFont* customFont3 = [UIFont fontWithName:#"HelveticaNeue-Thin" size:12.0];
UIFont* customFont4 = [UIFont fontWithName:#"MyriadPro" size:12.0];
UIFont* customFont5 = [UIFont fontWithName:#"MyriadPro-Italic" size:12.0];
UIFont* customFont6 = [UIFont fontWithName:#"MyriadPro-Condensed" size:12.0];
I would like to know which UIFont's are system. I practically need a method that would return a BOOL YES for variables: systemFont1, systemFont2, systemFont3 and NO for customFont4, customFont5, customFont6.
Since Helvetica Neue is system font on iOS7, this is subject of a debate, whether it should return NO or YES in these cases, but for my issue, it would be fine either way.
So my question is:
How to verify if UIFont instance was created by either of system font methods?
Thank you for your help!
Here is your method you want:
-(BOOL)isSystemFont:(UIFont *)font
{
return ([[font familyName] isEqualToString:[[UIFont systemFontOfSize:12.0f] familyName]])?YES:NO;
}
Or as an extension in Swift3
extension UIFont {
func isSystemFont() -> Bool {
return self.familyName == UIFont.systemFont(ofSize: 12.0).familyName
}
}
Above method will return as you need
if([self isSystemFont:systemFont1]) NSLog(#"SystemFont");
else NSLog(#"Custom Font");
if([self isSystemFont:customFont1]) NSLog(#"SystemFont");
else NSLog(#"Custom Font");
Output is
2014-03-04 15:48:18.791 TestProject[4031:70b] SystemFont
2014-03-04 15:48:18.791 TestProject[4031:70b] Custom Font
Fonts consist of more than name and size. Here's a more thorough check. Caveat: only work on iOS 7.
BOOL FontIsEqualToFont(UIFont *font1, UIFont *font2)
{
return [[[font1 fontDescriptor] fontAttributes] isEqual:[[font2 fontDescriptor] fontAttributes]];
}
// use as follows:
BOOL isSystemFont = FontIsEqualToFont(systemFont1, customFont1);
Another good way to uniquely identify fonts is to use their postscript names:
BOOL fontsAreEqual = [[[font1 fontDescriptor] postScriptName] isEqualToString:[[font2 fontDescriptor] postScriptName]];
Just check fontName and/or familyName?
NSLog(#"Fontname: %#", systemFont1.fontName);
NSLog(#"Familyname: %#", systemFont1.familyName);
I have created method and tested in both iOS 6 and iOS7. It is working fine. I was getting result what you are expected.
Here is Code:
- (BOOL)isSystemFont:(UIFont*)pFont {
NSLog(#"pFont-%#",pFont.familyName);
// Object created for comparing system font with custom font
UIFont *font = [UIFont systemFontOfSize:10];
NSLog(#"Font-%#",font.familyName);
if ([pFont.familyName isEqualToString:font.familyName]) {
return YES;
} else {
return NO;
}
}
Hope, It will may helpful to you.
Related
Trying to display super/subscript text using NSAttributedString in a UITextView seems broken in iOS13 - unless anyone knows otherwise?
Curiously if I use the UIFont systemFont then it works - but if I use any other font it doesn't.
See below for my code to setup a UITextView in my test app.
- (void)viewDidLoad
{
[super viewDidLoad];
UIFont* font = [UIFont systemFontOfSize:32];
//UIFont* font = [UIFont fontWithName:#"Helvetica Neue" size:32];
//UIFont* font = [UIFont fontWithName:#"Courier" size:32];
//UIFont* font = [UIFont fontWithName:#"Arial" size:32];
NSMutableAttributedString* as = [[NSMutableAttributedString alloc] initWithString:#"Super2Script" attributes:#{NSFontAttributeName : font}];
[as addAttribute:(NSString*)kCTSuperscriptAttributeName value:#(1) range:NSMakeRange(5, 1)];
UITextView* tv = [[UITextView alloc] initWithFrame:CGRectZero];
tv.attributedText = as;
[tv sizeToFit];
[self.view addSubview:tv];
tv.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
}
Simple 'fix' for this one.
It appears kCTSuperscriptAttributeName no longer works in iOS13 (for non-system fonts.) You need to use NSSuperscriptAttributeName instead. No idea where the definition for this lives (which header) so the actual string value required is "NSSuperScript"
how to change font size of label without changing font family in xcode6 ?I am using following line of code:
lblMain.font = [UIFont systemFontOfSize:22];
Here's an example:
lblMain.font = [UIFont fontWithName:#"HelveticaNeue" size:22];
Or - you can even use the current font:
lblMain.font = [lblMain.font fontWithSize:22];
NSString *fontName = lblMain.font.fontName;
CGFloat fontSize = lblMain.font.pointSize;
[lblMain setFont:[UIFont fontWithName:fontName size:NEWSIZE]];
Use following code, if you just want to change font size keeping the same font family:
lblMain.font = [UIFont fontWithName:lblMain.font.fontName size:22.0f];
OR
You can also use this code:
lblMain.font = [lblMain.font fontWithSize:22.0f];
[lblMain setFont:[UIFont fontWithName:#"HelveticaNeue" size:17]];
The following code works fine in iOS 7, but doesn't return bold or italic font in iOS 8. It is ok for Helvetica Neue, but doesn't work for Arial font.
UIFontDescriptor *descriptor1 = [UIFontDescriptor fontDescriptorWithFontAttributes:#{UIFontDescriptorFamilyAttribute: #"Arial"}];
UIFontDescriptor* boldFontDescriptor1 = [descriptor1 fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
UIFont* font1 = [UIFont fontWithDescriptor:boldFontDescriptor1 size:16.0];
[self.lblArialB setFont:font1];
Tested on device and simulator and still same error.
FWIW, this is the workaround I came up with, using UIFontDescriptor's attributes dictionary initializer instead of the seemingly buggy fontDescriptorWithSymbolicTraits:
NSString *fontFamily = #"Arial";
BOOL isBold = YES;
BOOL isItalic = YES;
CGFloat fontSize = 20.0;
UIFontDescriptor *fontDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes:
#{
#"NSFontFamilyAttribute" : fontFamily,
#"NSFontFaceAttribute" : (isBold && isItalic ? #"Bold Italic" : (isBold ? #"Bold" : (isItalic ? #"Italic" : #"Regular")))
}];
UIFont *font = [UIFont fontWithDescriptor:fontDescriptor size:fontSize];
For me, the fontDescriptorWithFontAttributes is working as before but the fontDescriptorWithSymbolicTraits fails miserably.
I solved it by going back to using [UIFont fontWithName: size:]
[self.titleLabel setFont:[UIFont fontWithName:#"PTSans-Bold" size:47.67]];
I am trying to change the text color in viewForRow based on a certain condition. When I press a button the view changes to a different color but I would also like to change the colour of the text in the picker. I use 'viewForRow' because I have a custom view.
//adds subcategories to the wheel
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
label.backgroundColor = [UIColor clearColor];
if (!self.isBackgroundWhite)//Boolean that changes when the background changes
{
NSLog(#"Set white text");
label.textColor = [UIColor whiteColor];
}else{
label.textColor = [UIColor blackColor];
}
if (row == 1) {
label.text = [NSString stringWithFormat:#" %#",[self.servicesArray objectAtIndex:row]];
label.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18];
}else{
label.text = [NSString stringWithFormat:#" -%#",[self.servicesArray objectAtIndex:row] ];
label.font = [UIFont fontWithName:kAppFont size:16];
}
return label;
}
EDIT: Thanks to #Rich I was able to spot my problem, I just need to call [self.pickerView reloadAllComponents];
If you only want to change the text color just use the other delegate method pickerView:attributedTitleForRow:forComponent:
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *title;
UIFont *font;
if (row == 1) {
title = [NSString stringWithFormat:#" %#",[self.servicesArray objectAtIndex:row]];
font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18];
} else{
title = [NSString stringWithFormat:#" -%#",[self.servicesArray objectAtIndex:row] ];
font = [UIFont fontWithName:kAppFont size:16];
}
UIColor *textColor;
if (self.isBackgroundWhite) {
textColor = [UIColor blackColor];
} else {
NSLog(#"Set white text");
textColor = [UIColor whiteColor];
}
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary *attributes = #{NSForegroundColorAttributeName : textColor,
NSFontAttributeName : font,
NSParagraphStyleAttributeName : paragraphStyle};
return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}
Also a note that you should call [self.pickerView reloadAllComponents]; when changing isBackgroundWhite. I would do this by overriding the setter for backgroundWhite and when the boolean value changes reload the picker view.
EDIT:
There appears to be a 'bug' (intentional or not) in iOS 7 (works fine on iOS 6) with setting the UIFont on an NSAttributedString returned from the pickerView:attributedTitleForRow:forComponent: method. So while the above works for the text color the font does not change. Luckily you can get around this with the code in the question. See this answer for more info.
i have problem. I have dynamic tableview. So i need change text size in my textLabel.
I tried:
one:
cell.textLabel.font = [UIFont systemFontOfSize:30.0f];
two:
cell.textLabel.font = [UIFont boldSystemFontOfSize:12.0];
three:
UIFont *myFont = [ UIFont fontWithName: #"Arial" size: 30.0 ];
cell.textLabel.font = myFont;
But my textlabel not changing. So detailtextlabel - change perfectly but text label - no. What i do wrong? thanks
You can try with the following..
but make sure you write this code before u setting your text
cell.textLabel.text=#"Your TEXT Goes Here";
UIFont *myFont = [ UIFont fontWithName: #"Arial" size: 18.0 ];
cell.textLabel.font = myFont;
hope it helps.