I want to change all system font to custom font at one place with storyboard and xib.
I have tried below code to change font.
[[UILabel appearance] setFont:[UIFont fontWithName:#"fontName" size:17.0]];
[[UITextField appearance] setFont:[UIFont fontWithName:#"fontName" size:17.0]];
When I used above code its working fine but its changing size also.
I just want to change font family and keep size as it has in storyboard or xib.
Please provide me some good solution.
I think you can keep using the same statements, but instead of setting the size using a literal of your choice, just use the current system font size like this:
float currentSystemFontSize = [UIFont systemFontSize];
[[UILabel appearance] setFont:[UIFont fontWithName:#"fontName" size:currentSystemFontSize]];
This should keep the system's font size and only change the font family.
Check this answer for more information about how to get system font size. Good luck!
call below methods in ViewDidLoad():
[self setCustomFontFamilyNotSize:#"CustomFontName" forView:self.view andSubViews:YES];
Method Dealarations:
-(void)setCustomFontFamilyNotSize:(NSString*)customFontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews
{
if ([view isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel *)view;
[lbl setFont:[UIFont fontWithName:customFontFamily size:[[lbl font] pointSize]]];
}
if ([view isKindOfClass:[UITextField class]])
{
UITextField *txt = (UITextField *)view;
[txt setFont:[UIFont fontWithName:customFontFamily size:[[txt font] pointSize]]];
}
}
Related
Can Change UITextField Placeholder Text Color as follows:
[txtField setValue:[UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0]
forKeyPath:#"_placeholderLabel.textColor"];
Similarly is there any way to Resize UITextField Placeholder Text Font to Fit UITextField width?
Note: I have text fields in custom cell of table view and also using autolayout in custom cell.
I met the same issue, here is the solution:
UILabel *placeHolderLabel = [self.registrationCodeTextfield valueForKey:#"_placeholderLabel"];
placeHolderLabel.adjustsFontSizeToFitWidth = YES;
Here is one coding that i used in ViewDidLoad to set the placeholder of textfield of desirable size.
-----------------------------------code-----------------------------------------
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
textfieldUsername.leftView = paddingView;
textfieldUsername.leftViewMode = UITextFieldViewModeAlways;
[[UITextField appearance] setTintColor:[UIColor whiteColor]];
[textfieldUsername setValue:[UIColor lightTextColor] forKeyPath:#"_placeholderLabel.textColor"];
(Nj)
You have the placeholder text and you have the frame of the uitextfield .
From these two you can calculate the font size which fits and assign as a Attributed Placeholder String.
If calculated font size is less than your limit , then use that font size other wise use the limit
Here is code
textField.placeholder = #"Pavankumar";
[textField setValue:[UIColor greenColor]
forKeyPath:#"_placeholderLabel.textColor"];
see below code it change font of the placeholder
textField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:#"Pavankumar" attributes:#{NSForegroundColorAttributeName:[UIColor greenColor],NSFontAttributeName:[UIFont fontWithName:#"HelveticaNeue-light" size:12.0]}];
This seems like a no brainer, but I cannot find any way to do this. Basically what I have is a UISegmentedControl with two localized labels using NSLocalizedString. I have set the font size and everything looks great in English and a few other languages. But, in Japanese and other languages the characters are larger and cause the label to be truncated.
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
NSLocalizedString(#"Miles", nil).uppercaseString,
NSLocalizedString(#"Kilometers", nil).uppercaseString,
nil]];
self.segmentedControl.apportionsSegmentWidthsByContent = YES;
self.segmentedControl.selectedSegmentIndex = self.metric ? 1 : 0;
[self.segmentedControl addTarget:self action:#selector(changeMetric:) forControlEvents:UIControlEventValueChanged];
self.segmentedControl.frame = CGRectMake(8, middleHolder.frame.size.height/2+60, progressWidth, 30);
self.segmentedControl.center = CGPointMake(self.view.center.x, self.segmentedControl.center.y);
[self.segmentedControl setTitleTextAttributes:#{
NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue-UltraLight" size:36]
} forState:UIControlStateSelected];
[self.segmentedControl setTitleTextAttributes:#{
NSForegroundColorAttributeName: [[UIColor whiteColor] colorWithAlphaComponent:0.3],
NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue-UltraLight" size:36]
} forState:UIControlStateNormal];
self.segmentedControl.tintColor = [UIColor clearColor];
self.segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[middleHolder addSubview:self.segmentedControl];
Is there any way to scale the font size of the label depending on the label width? I realize that these are not normal UILabel's, so there is no adjustsFontSizeToFitWidth property.
I'm tackling with the same issue. Here is my solution (a bit rough around the edges):
I subclass the segmented control and then in my subclass, do the following. Hope this helps! (Tested on iOS 7 and 8)
+ (void)setSublabelScale:(UIView *)view {
for (id subview in view.subviews) {
if ([subview isKindOfClass:[UILabel class]]) {
[subview setMinimumScaleFactor:0.5];
[subview setAdjustsFontSizeToFitWidth:YES];
} else {
[MYSegmentedControl setSublabelScale:subview];
}
}
}
- (void)layoutSubviews {
[super layoutSubviews];
[MYSegmentedControl setSublabelScale:self];
}
I found out that NSAttributedString has a size property to it, that will allow me to know how wide the text is for every local. So, I can just do something like:
CGFloat fontSize = 36;
NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:NSLocalizedString(#"Kilometers", nil).uppercaseString attributes:#{NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue-UltraLight" size:fontSize]}];
if (attributedString.size.width > 200) {
fontSize = 30;
}
I got a UISegmentedControl called mySegmentedControl
#property (nonatomic) IBOutlet UISegmentedControl *mySegmentedControl;
with 3 segments, and I would change text font for each segment.. Is possible?
Yes you can ----- Try this
for(uint i=0;i<[mySegmentedControl subviews].count;i++)
{
for(UIView *view in [[[mySegmentedControl subviews] objectAtIndex:i] subviews])
{
if([view isKindOfClass:[UILabel class]])
{
if(i==0) // set First segment font
[(UILabel*)view setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:27]];
if(i==1) // set Second segment font
[(UILabel*)view setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:17]];
if(i==2) // set Third segment font
[(UILabel*)view setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:7]];
}
}
}
You can use any images instead of text. This will give you flexible customization for appearance.
NSArray *segemtImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"1st.png"],[UIImage imageNamed:#"2nd.png"],[UIImage imageNamed:#"3rd.png"], nil];
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:segemtImages];
I don't think that is possible. Your only options are either use a custom open source implementation, or set a custom image with the text for each segment.
I modify #Anand Natan 's code:
In storyboard set the first label "1111111111111" is to change it default width. Because change the label don't change segmentcontroll size.
for(uint i=0;i<[_menuBarTop subviews].count;i++){
for(uint j=0;j<[[[[_menuBarTop subviews] objectAtIndex:i] subviews]count];j++){
UIView* view = [[[_menuBarTop subviews] objectAtIndex:i] subviews][j];
if([view isKindOfClass:[UILabel class]]){
UILabel* label = (UILabel*)view;
if([label.text isEqualToString:#"1111111111111"]){
label.text = LANGLOC(#"videolist_category_choosen");
}else if([label.text isEqualToString:#"2"]){
label.text = LANGLOC(#"videolist_category_followed");
}
break;
}
}
}
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:12], NSFontAttributeName, nil];
[_sgmntStatusUI setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
for ios7 later varsions
I want to use a custom font in whole app (ios 5). I used the following code
[[UILabel appearance] setFont:[UIFont fontWithName:#"MyCustomFont" size:17.0]];
The problem with this is that it overrides all font sizes in all views.
How can i avoid it?
Create category for UILabel and add the following code over there.
#implementation UILabel (CustomFontLabel)
-(void)awakeFromNib{
float size = [self.font pointSize];
self.font = [UIFont fontWithName:#"MyCustomFont" size:size];
}
#end
I'm trying to implement a control for deleting entire sections, and it would look best in my app if the delete button was in the header, as opposed to an overlay like a UIPopoverView.
In the process of writing this question, I found the answer. Easy enough, once there's a starting point.
I got the bulk of the code from this blog which has only two posts, both from 2010.
Then I went back to this site just for the font color, since it's more trouble to break apart.
Three minor problems, all with the label.
- Font is too narrow
- Text color is too dark
- Label origin is wrong
The default font is known, so that comes first.
label.font = [UIFont boldSystemFontOfSize:17.0];
Color is next, since that's easy. Used an image editor's Eyedropper tool for this.
label.textColor = [UIColor colorWithRed:0.298 green:0.337 blue:0.423 alpha:1];
// Is there a difference between alpha:1 and alpha:1.000?
Then the hard part. A close guess, and then some tweaking for a perfect match.
label.frame = CGRectMake(54, 4, headerView.frame.size.width-20, 22);
And now we have a custom implementation that perfectly matches the current Grouped header.
Finished code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
tableView.sectionHeaderHeight = headerView.frame.size.height;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(54, 4, labelSize.width, labelSize.height)];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont boldSystemFontOfSize:17.0]];
[label setShadowColor:[UIColor whiteColor]];
[label setShadowOffset:CGSizeMake(0, 1)];
[label setText:[self tableView:tableView titleForHeaderInSection:section]];
[label setTextColor:[UIColor colorWithRed:0.298 green:0.337 blue:0.423 alpha:1.000]];
[headerView addSubview:label];
return headerView;
}
Found this SO answer after finding the right font/color myself. Oh well.
Edit:
For a title label that allows an effectively unlimited amount of text:
// before label init
NSString *title = [self tableView:tableView titleForHeaderInSection:section];
NSUInteger maxWidth = headerView.frame.size.width-108;
CGSize labelSize = [title sizeWithFont:[UIFont systemFontOfSize:17.0]
constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX)];
if (labelSize.width < maxWidth) labelSize.width = maxWidth;
// after setFont:
[label setNumberOfLines:0];