我正在尝试以编程方式设置UITextField的字体。问题在于它以一种奇怪的方式运作。
我在UITableViewController中有多个UITextField。
我尝试以这种方式设置字体
[self.textfieldProspect setFont:nil];
[self.textfieldProspect setFont:[UIFont systemFontOfSize:15]];
我将上面的代码放在ViewDidLoad,ViewWillAppear和ViewDidLayoutSubviews中。
它仅适用于ViewDidLayoutSubviews。这导致textField在视图控制器变为可见时可视地更改其字体。我不希望这样,我希望在ViewController变为可见之前设置字体。
我以模态方式呈现viewController。
无论如何在textField变得可见之前更改字体。这听起来像一个微不足道的问题,但我尝试了一切,似乎没有任何工作。
这是完整的代码
#import <UIKit/UIKit.h>
@protocol AddActivityViewControllerDelegate;
@interface AddActivityViewController : UITableViewController
@end
- (void)viewDidLoad
{
[super viewDidLoad];
self.themeManager = [ThemeManager sharedThemeManager];
[self.themeManager applyAppBackgroundColorToView:self.view];
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
self.textviewNotes.text = @"";
self.textviewNotes.placeholder = @"Notes";
self.textviewNotes.placeholderColor = [CommonHelper getSelectedVarientPlaceHolderColor];
[self setUpTextFieldsPaddingView];
self.dateFormatter = [CommonHelper getDateFormatterWithFormat:@"MMM d, yyyy h:mm a"];
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar setBarStyle:UIBarStyleBlackTranslucent];
[toolbar sizeToFit];
UIBarButtonItem* doneButton =[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboardCustom:)];
NSArray *itemsArray = [NSArray arrayWithObjects:doneButton,nil];
[toolbar setItems:itemsArray animated:NO];
[self.fieldEndDate setInputAccessoryView:toolbar];
[self.fieldStartDate setInputAccessoryView:toolbar];
self.datePicker = [[UIDatePicker alloc] init];
self.datePicker.datePickerMode = UIDatePickerModeDateAndTime;
//self.datePicker.minimumDate = [NSDate date];
[self.datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
self.fieldStartDate.inputView = self.datePicker;
self.fieldEndDate.inputView = self.datePicker;
UIView* seperatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, self.navigationController.navigationBar.frame.size.width, 1)];
seperatorView.backgroundColor = [UIColor lightGrayColor];
[self.navigationController.navigationBar addSubview:seperatorView];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0,0,64,30);
[btn setBackgroundColor:[UIColor darkGrayColor]];
[btn setTitle:@"Cancel" forState:UIControlStateNormal];
btn.titleLabel.font = [self.themeManager getSelectedFont];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(hideViewController) forControlEvents:UIControlEventTouchUpInside];
btn.layer.cornerRadius = 3.0f;
UIBarButtonItem *barBackBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0,0,64,30);
[btn setBackgroundColor:[UIColor darkGrayColor]];
[btn setTitle:@"Done" forState:UIControlStateNormal];
btn.titleLabel.font = [self.themeManager getSelectedFont];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(doneButtonTapped) forControlEvents:UIControlEventTouchUpInside];
btn.layer.cornerRadius = 3.0f;
UIBarButtonItem *barDoneBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = barDoneBtn;
self.navigationItem.leftBarButtonItem = barBackBtn;
//Data Models
///////////////////////////////////////////////////////////////////
Up_DateAppDelegate* appDelegate = (Up_DateAppDelegate*)[UIApplication sharedApplication].delegate;
self.managedObjectContext = appDelegate.managedObjectContext;
self.prospect = [appDelegate GetSelectedProspect];
NSLog(@"nickname%@",self.prospect.nickName);
NSLog(@"firstname%@ ",self.prospect.firstName);
NSLog(@"lastname %@",self.prospect.lastName);
self.btnDeleteActivity.hidden = YES;
self.selectedStartDate = [NSDate date];
isStartDateSelected = YES;
self.fieldStartDate.text = [self.dateFormatter stringFromDate:self.selectedStartDate];
if (self.isCalendarMode == YES)
{
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults synchronize];
NSDate* startDate = [userDefaults objectForKey:@"CurrentSelectedMonthForCalendar"];
//Getting current time
NSDate* currentTime = [NSDate date];
NSCalendar* theCalendar = [NSCalendar currentCalendar];
NSDateComponents *dayComponent = [theCalendar components:( NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate:startDate];
//[dayComponent setTimeZone:[NSTimeZone defaultTimeZone]];
NSDateComponents* timeComponents = [theCalendar components:(NSCalendarUnitHour) fromDate:currentTime];
[dayComponent setHour:timeComponents.hour];
[dayComponent setMinute:0];
[dayComponent setSecond:0];
startDate = [theCalendar dateFromComponents:dayComponent];
self.initiallySelectedDateForCalendarMode = startDate;
self.selectedStartDate = startDate;
self.fieldStartDate.text = [self.dateFormatter stringFromDate:startDate];
//Autocomplete Search options for prospects
activityProspectsAutocompleteVC = [ActivityProspectsAutocompleteViewController createAndSetupAutoCompleteWithDisplayViewController:self textfield:self.textfieldProspect];
[self.view addSubview:tableviewAutoCompleteProspects];
}
self.fieldEndDate.text = @"";
self.resultLevelSelected = @"None";
self.repeatCountSelected = 0;
self.repeatOptionSelected = @"Never";
self.alertOptionSelected = @"None";
[self.timeInvestmentLabel setFont:[UIFont fontWithName:[CommonHelper getBoldFontForFontName:[self.themeManager getSelectedFontName]] size:[self.themeManager getBiggerFontSize]]];
[self.timeInvestmentLabel setTextColor:[UIColor whiteColor]];
[self.resultLabel setFont:[UIFont fontWithName:[CommonHelper getBoldFontForFontName:[self.themeManager getSelectedFontName]] size:[self.themeManager getBiggerFontSize]]];
[self.resultLabel setTextColor:[UIColor whiteColor]];
self.timeInvestmentLabel.attributedText = [[NSAttributedString alloc] initWithString:@"Time Investment" attributes:@{ NSStrokeColorAttributeName : [UIColor blackColor], NSForegroundColorAttributeName : [UIColor whiteColor], NSStrokeWidthAttributeName : @-3.5}];
self.resultLabel.attributedText = [[NSAttributedString alloc] initWithString:@"Result?" attributes:@{ NSStrokeColorAttributeName : [UIColor blackColor], NSForegroundColorAttributeName : [UIColor whiteColor], NSStrokeWidthAttributeName : @-3.5}];
NSAttributedString *attributedText =
[[NSAttributedString alloc] initWithString:@" Result"
attributes:@{NSStrokeWidthAttributeName: [NSNumber numberWithInt:-3],
NSStrokeColorAttributeName: [UIColor lightGrayColor],
NSForegroundColorAttributeName: [UIColor whiteColor]}];
[self.btnResultLevel setAttributedTitle:attributedText forState:UIControlStateNormal];
if (self.isCalendarMode == NO)
{
//coming from prospect
self.textfieldProspect.userInteractionEnabled = NO;
}
NSString* pageTitle = @"";
if (self.editModeEnabled == NO)
{
//Add Activity for a Pospect
self.textfieldProspect.text = [CommonHelper getDisplayNameForProspec:self.prospect];
[self.textfieldProspect setFont:nil];
[self.textfieldProspect setFont:[self.themeManager getSelectedBiggerFont]];
if (self.isCalendarMode == YES)
{
self.textfieldProspect.text = @"";
[self.textfieldProspect setFont:nil];
[self.textfieldProspect setFont:[self.themeManager getSelectedBiggerFont]];
}
pageTitle = @"Add Activity";
}
else
{
//Edit Mode
[self setUpFieldsValuesFromDataModel];
pageTitle = @"Edit Activity";
}
// Creating custom title view so that we can apply font of our choice
CGRect frame = CGRectMake(0, 0, 200, 21);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:[CommonHelper getBoldFontForFontName:[self.themeManager getSelectedFontName]] size:[self.themeManager getBiggerFontSize]];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = pageTitle;
self.navigationItem.titleView = label;
//Delete Activity button
[self.btnDeleteActivity.layer setShadowColor:[UIColor blackColor].CGColor];
[self.btnDeleteActivity.layer setShadowOpacity:1.0];
[self.btnDeleteActivity.layer setShadowRadius:3.0];
[self.btnDeleteActivity.layer setShadowOffset:CGSizeMake(0,3.0)];
self.btnDeleteActivity.layer.masksToBounds = NO;
self.btnDeleteActivity.layer.cornerRadius = 3.0f;
}
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[self.textfieldProspect setFont:nil];
[self.textfieldProspect setFont:[self.themeManager getSelectedBiggerFont]];
[self.textfieldActivity setFont:nil];
[self.textfieldActivity setFont:[self.themeManager getSelectedBiggerFont]];
[self.textfieldLocation setFont:nil];
[self.textfieldLocation setFont:[self.themeManager getSelectedBiggerFont]];
[self.textfieldWebsite setFont:nil];
[self.textfieldWebsite setFont:[self.themeManager getSelectedBiggerFont]];
[self.fieldEndDate setFont:nil];
[self.fieldEndDate setFont:[self.themeManager getSelectedBiggerFont]];
[self.fieldStartDate setFont:nil];
[self.fieldStartDate setFont:[self.themeManager getSelectedBiggerFont]];
[self.textfieldMonetaryInvestment setFont:nil];
[self.textfieldMonetaryInvestment setFont:[self.themeManager getSelectedBiggerFont]];
}