0

I try to change a font size in my UITextView and every time I change the attributedText was restore to default.

with this I make the middle test bold :

str = [[NSMutableAttributedString alloc] initWithAttributedString:string];

[str addAttribute:NSFontAttributeName value:newFont range:NSMakeRange(range.location, range.length)];

before: that was bold font in the middle.

after :all the text bold

enter image description here

I want that after the size change the bold stay where he be.

Edit :

I try with this two ways :

1)

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:currentTextViewEdit.text];
        [attributedString addAttribute:NSFontAttributeName  value:[UIFont systemFontOfSize:textSize] range:NSMakeRange(0, currentTextViewEdit.text.length)];
        currentTextViewEdit.attributedText = attributedString;

2)

currentTextViewEdit.font = [UIFont   fontWithName:currentTextViewEdit.font.fontName size:textSize];
Larme
  • 24,190
  • 6
  • 51
  • 81
Roei Nadam
  • 1,780
  • 1
  • 15
  • 33

2 Answers2

2

As reported by @matt:
aUITextView.font => change on text property (plain text)
So switching between attributedText and text like you're doing is causing the issue.

On your other solution:
Bold effect is inside the NSFontAttributeName, it's the Font value. So if you replace it by a common font, that will remove the "bold" effect. Same for italic.

So a way to do it (not tested, by according to you it's working):
• Save the attributedText (to remember the "effects", like bold in your case)
• Save the cursor position (I didn't test, so I don't know where would go the cursor afterward)
• Change the font size keeping the previous font (may be bold of normal) to the attributed string
• Update the attributedText
• Replace the cursor

You can use a Category on UITextView with a method -(void)changeFontSize:(float)newFontSize; and then you call self instead of currentTextViewEdit.

NSMutableAttributedString *attributedString = [[currentTextViewEdit attributedText] mutableCopy];

//Save the cursor/selection
NSRange cursorRange = [currentTextViewEdit selectedRange];

//Apply to update the effects on new text typed by user?
[currentTextViewEdit setFont:[UIFont fontWithName:[[currentTextViewEdit font] fontName] size:newFontSize]];

//Update the font size
[attributedString enumerateAttribute:NSFontAttributeName
                             inRange:NSMakeRange(0, [attributedString length])
                             options:0
                          usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
                              UIFont *currentfont = (UIFont*)value;
                              UIFont *newFont = [UIFont fontWithName:[currentfont fontName] size:newFontSize];
                              [attributedString addAttribute:NSFontAttributeName value:newFont range:range];
                          }];
[currentTextViewEdit setAttributedText:attributedString];

//To restaure the cursor/selection
[currentTextViewEdit setSelectedRange:cursorRange];
Larme
  • 24,190
  • 6
  • 51
  • 81
1

It isn't clear what your question is. Your observation, however, is exactly correct: If you are using an attributed text (attributedText), then if you change a pure text feature of the text view (like font), it resets the attributed text. That's because you're not supposed to do that. You must not attempt to combine attributed text features with plain text features. Use the plain text features (like font) only if you are using a plain text.

If, for example, you want to change the font when you're using attributed text, change the font of the attributed text. To do so, pull out the attributedText, put it into an NSMutableAttributedString, make your change, and assign it back into the attributedText.

matt
  • 515,959
  • 87
  • 875
  • 1,141