如何在xCode中更改选择器文本的颜色?

时间:2016-02-29 12:23:52

标签: ios objective-c

我终于有两个采摘者进入一个视图控制器,我意识到他们看起来有点棘手,因为背景很暗。如何更改拣配文本的颜色?

这是我的整个视图控制器M

    //
//  ViewController.m
//  Repayment Calculator
//
//  Created by Stewart Piper-Smith on 04/11/2015.
//  Copyright (c) 2015 Stewart Piper-Smith. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()


@end

NSArray *_pickerViewArray;

NSArray *CareerViewArray;

@implementation ViewController



@synthesize button1, textbox1, textbox2, textbox3, startingsalary, repaymentlabel, debtlabel, startingrepayments, timetakentopayoffloan, realtimetakentopayoffloan, debtamounttextfield, annualrepaymentstextfield, monthlyrepaymentstextfield,weeklyrepaymentstextfield, payoffloantextfield, testlabel, annualrepaymentstest, totaldebtamounttest, writtenofflabel,payoffloanlabel,newlabel;


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// - (IBAction)button1Press:(id)sender {
//    [button1 setTitle:textbox1.text forState:UIControlStateNormal];
// }

//The start of the calculate button that starts the calculations.

-(IBAction)menucontactbuttonPress:(id)sender {

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error"
                                                                             message:@"Contact Form Coming Soon"
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    //We add buttons to the alert controller by creating UIAlertActions:
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                       style:UIAlertActionStyleDefault
                                                     handler:nil]; //You can use a block here to handle a press on this button
    [alertController addAction:actionOk];
    [self presentViewController:alertController animated:YES completion:nil];

}

 -(IBAction)button1Press:(id)sender

{
    [self.view endEditing:YES];
    int x = ([textbox1.text intValue]);
    int y = ([textbox2.text intValue]);
    int q = ([textbox3.text intValue]);
    float startingsal = ([startingsalary.text intValue]);


    int debtamounttfield = ([debtamounttextfield.text intValue]);

    float annualrepayments = ([annualrepaymentstextfield.text floatValue]);

    //PROBLEM STARTS
    float p1 = debtamounttfield /annualrepayments;//([debtamounttextfield.text intValue]);

    if(isnan(p1) || isinf(p1)){
        p1 = 0.00;
    }

    //int a = ([annualrepaymentstextfield.text intValue]);
    //int b = ([debtamounttextfield.text intValue]);

    //[newlabel setText:[NSString stringWithFormat:@"%@", a/b]];

    [testlabel setText:[NSString stringWithFormat:@"%.01f",p1]];

    [totaldebtamounttest setText:[NSString stringWithFormat:@"%d", debtamounttfield]];

    [annualrepaymentstest setText:[NSString stringWithFormat:@"%@", annualrepaymentstextfield]];

    [payoffloantextfield setText:[NSString stringWithFormat:@"%.01f",  p1]];
    //PROBLEM ENDS

    [debtamounttextfield setText:[NSString stringWithFormat:@"£" "%i" , (x + y) * q]];

    int arp = (startingsal - 17335)*0.09;

    [annualrepaymentstextfield setText:[NSString stringWithFormat:@"£" "%i",  arp ]];

    if (arp < 0) {

        annualrepaymentstextfield.text = @"Written Off";

    }

    int monthlyrepayments = (startingsal - 17335)*0.09/12;

    [monthlyrepaymentstextfield setText:[NSString stringWithFormat:@"£" "%i",  monthlyrepayments]];

    if (monthlyrepayments < 0){

        monthlyrepaymentstextfield.text = @"Written Off";
    }

    int weeklyrepayments = (startingsal - 17335)*0.09/12/4;

    [weeklyrepaymentstextfield setText:[NSString stringWithFormat:@"£" "%i",  weeklyrepayments]];

    if (weeklyrepayments < 0){

        weeklyrepaymentstextfield.text = @"Written Off";
    }

    //Error Handling
    //Enter starting salary
    if ([annualrepaymentstextfield.text isEqualToString: @"Written Off"]) {

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Debt Written Off"
                                                                                 message:@"As it will take you longer than 30 years to pay off the loan, the debt would be written off."
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        //We add buttons to the alert controller by creating UIAlertActions:
        UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                           style:UIAlertActionStyleDefault
                                                         handler:nil]; //You can use a block here to handle a press on this button
        [alertController addAction:actionOk];
        [self presentViewController:alertController animated:YES completion:nil];


    }

    if ([startingsalary.text isEqualToString: @""]) {

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error"
                                                                                 message:@"Please enter your expected starting salary"
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        //We add buttons to the alert controller by creating UIAlertActions:
        UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                           style:UIAlertActionStyleDefault
                                                         handler:nil]; //You can use a block here to handle a press on this button
        [alertController addAction:actionOk];
        [self presentViewController:alertController animated:YES completion:nil];


    }

    //If course length is equal to 0
    if ([textbox3.text isEqualToString: @"0"]) {

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error"
                                                                                 message:@"Your course lenth cannot be 0 Years. Please enter a different value"
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        //We add buttons to the alert controller by creating UIAlertActions:
        UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                           style:UIAlertActionStyleDefault
                                                         handler:nil]; //You can use a block here to handle a press on this button
        [alertController addAction:actionOk];
        [self presentViewController:alertController animated:YES completion:nil];
    }


    //If course length text field is empty
    if ([textbox3.text isEqualToString: @""]){
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error"
                                                                                 message:@"Please enter your current course length"
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        //We add buttons to the alert controller by creating UIAlertActions:
        UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                           style:UIAlertActionStyleDefault
                                                         handler:nil]; //You can use a block here to handle a press on this button
        [alertController addAction:actionOk];
        [self presentViewController:alertController animated:YES completion:nil];


    }

    //time taken to pay off loan > 20 years
    int newtimetaken = [timetakentopayoffloan.text intValue];

    if (newtimetaken <= 20) {

        writtenofflabel.text = @"WO";
    }

}




-(IBAction)clearbutton:(id)sender {

    [textbox1 setText:[NSString stringWithFormat:@""]];

    [textbox2 setText:[NSString stringWithFormat:@""]];

    [textbox3 setText:[NSString stringWithFormat:@""]];


    [startingrepayments setText:[NSString stringWithFormat:@"Loan at start of repayments: "]];

    [startingsalary setText:[NSString stringWithFormat:@""]];

    [realtimetakentopayoffloan setText:[NSString stringWithFormat:@""]];

    [debtamounttextfield setText:[NSString stringWithFormat:@""]];

    [annualrepaymentstextfield setText:[NSString stringWithFormat:@""]];

    [monthlyrepaymentstextfield setText:[NSString stringWithFormat:@""]];

    [weeklyrepaymentstextfield setText:[NSString stringWithFormat:@""]];

    [payoffloantextfield setText:[NSString stringWithFormat:@""]];

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Cleared"
                                                                             message:@"All fields have now been cleared."
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    //We add buttons to the alert controller by creating UIAlertActions:
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                       style:UIAlertActionStyleDefault
                                                     handler:nil]; //You can use a block here to handle a press on this button
    [alertController addAction:actionOk];
    [self presentViewController:alertController animated:YES completion:nil];
}



- (void)viewDidLoad {
    [super viewDidLoad];



    myLabel.text = @"Use the scroller above...";

    datePickerView.delegate = self;
    //CareerPickerView.dataSource = self;

    _pickerViewArray = @[@"Before September 2012",@"After September 2012"];



    CareerViewArray = @[@"1000",@"2000"];

    CareerPickerView.delegate = self;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if(pickerView == datePickerView)
    {
        NSString *dateSelected = [_pickerViewArray objectAtIndex:row];
        myLabel.text = dateSelected;
    }
    else if(pickerView == CareerPickerView)
    {
        NSString *careerSelected = [CareerViewArray objectAtIndex:row];
        startingsalary.text = careerSelected;
    }
}


-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

    if(pickerView == datePickerView)
    {
        return _pickerViewArray.count;
    }
    else if(pickerView == CareerPickerView)
    {
        return CareerViewArray.count;
    }
    return 1;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if(pickerView == datePickerView)
    {
        return _pickerViewArray[row];
    }
    else if(pickerView == CareerPickerView)
    {
        return CareerViewArray[row];
    }
    return nil;
}






@end

2 个答案:

答案 0 :(得分:0)

您可能需要实施UIPickerViewDelegate方法-pickerView:attributedTitleForRow:forComponent:

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        NSString *myTitle = @"my title";
        NSAttributedString *myAttString = [[NSAttributedString alloc] initWithString:myTitle attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
        return myAttString;
    }

参考:iOS Documentation

希望这有帮助。

答案 1 :(得分:0)

您可以使用UIPickerView委托方法:

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *title = self.pickerArray[row];
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:NEON_GREEN}];

    return attString;

}