Search code examples
iosxcodeuistepper

How can I change the FontSize by stepper in multiple scenses?


I am using a stepper to control the font size of a text view, but there is no action being fired until I press the stepper twice. Why is this happening?

The following code is for the `mystepper' IBAction:

- (IBAction) changeFontSize:(id)sender
{

    [myStepper setMinimumValue:14.0]
    self.myStepper.maximumValue =20.0;

    UIFont newSize = [myTextView fontWithSize:self.stepper.value];
    self.myTextView.font = newSize;

}

I find the problem is that i need set the current value of stepper equals to minimumvalue. However, I want the value of size can pass through in muiltple scences so that the fontSize doesn't need to be press overtime. How can i get it???


Solution

  • In viewDidLoad:

    [myStepper setMinimumValue:14.0]
    [myStepper setMaximumValue:20.0]
    
    UIFont newSize = [myTextView fontWithSize:self.stepper.value]; // provide some default value to myStepper
    self.myTextView.font = newSize;
    

    // assign one method to stepper - value changed event

    - (IBAction)stepperValueChanged:(id)sender
    {
        double stepperValue = ourStepper.value;
    
        [self.myTextView setFont:[self.myTextView.font fontWithSize:stepperValue]];
    }
    

    This is the core logic, but there might be minor changes reuqire, to satisfy your requirement.

    Hope this will help you.