1

I have a TextField and three buttons which are 40pts above the TextField. These buttons provide the changing of font size of TextField's text when I clicked on any of them for eg first button set font size to 17 second changes it to 20 and third change it to 24. So I add IbAction to all buttons like

   - (IBAction)setRegularText:(id)sender {
    self.additionalInfo.font = [UIFont systemFontOfSize:20];

}

And according to button. But it will change the previous entered text too. I want the text font to be change only when user selet the option. Previously entered text's font size must not be changed.

Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
naomi
  • 227
  • 1
  • 2
  • 9

3 Answers3

1

You will need to use the attributed string NSAttributedString. With text field it is best to have a delegate and implement the method on changing the characters in range. This will handle all the cases even when the user pasts the text from somewhere else.

So the NSMutableAttributedString has a method to replace the string in range with a mutable attributed string which is perfect for this method. The new string received by the delegate must simply be converted to the attributed one with a currently set font.

Try something like this:

@interface AttributedTextField : NSObject<UITextFieldDelegate>

@property (nonatomic, strong) NSMutableAttributedString *attributedString;
@property (nonatomic, strong) UIFont *currentFont;

@end

@implementation AttributedTextField

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // ensure having a font
    UIFont *font = self.currentFont;
    if(font == nil) {
        font = [UIFont systemFontOfSize:12.0f];
    }

    // ensure having a base string
    if(self.attributedString == nil) {
        self.attributedString = [[NSMutableAttributedString alloc] initWithString:@""];
    }

    // append the new string
    [self.attributedString replaceCharactersInRange:range withAttributedString:[[NSMutableAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName: font}]];

    textField.attributedText = self.attributedString; // assign the new text which is attributed

    return NO; // return false as we are overriding the text
}

@end
Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
0

set the tag of every button as the font size that button should change. i-e

self.button1.tag = 17;
self.button2.tag = 20;
self.button3.tag = 24;

and use the tag as font size. i-e

- (IBAction)setRegularText:(UIButton *)sender {
    self.additionalInfo.font = [UIFont systemFontOfSize:sender.tag];
}
M David
  • 260
  • 5
  • 18
0

You can set different text size in textfield like this way:

- (void)setFontString:(NSString *)setString setFontSize: (double) fontSize {

    self.txtAnswer.text = @"";
    self.txtAnswer.text = setString;
    self.txtAnswer.font = [UIFont systemFontOfSize:fontSize];
}

- (IBAction)btn1Tap:(id)sender {

    [self setFontString:@"Good Morning" setFontSize:20.0f];
}

- (IBAction)btn2Tap:(id)sender {

    [self setFontString:@"Good Afternoon" setFontSize:15.0f];
}

- (IBAction)btn3Tap:(id)sender {

    [self setFontString:@"Good Evening" setFontSize:10.0f];
}