更改字体大小而无需更改UITextView attributedText [英] Change font size without Change UITextView attributedText

查看:350
本文介绍了更改字体大小而无需更改UITextView attributedText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试更改UITextView中的字体大小,并且每次更改attributedText时都将其恢复为默认值.

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

我希望在更改大小后,将粗体停留在原处.

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

我尝试使用以下两种方法:

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];

推荐答案

由@matt报告:
aUITextView.font =>更改text属性(纯文本)
因此,像您一样在attributedTexttext之间切换会导致此问题.

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.

关于其他解决方案:
粗体效果在NSFontAttributeName内部,它是Font值.因此,如果将其替换为通用字体,将消除粗体"效果.斜体字也一样.

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.

一种做到这一点的方法(未经测试,根据您的说法,它正在工作):
•保存attributedText(记住效果",如您的情况下为粗体)
•保存光标位置(我没有测试,所以不知道之后光标会移到哪里)
•更改字体大小,将先前的字体(可能是普通字体加粗)更改为属性字符串
•更新attributedText
•更换光标

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

您可以将UITextView上的类别与方法-(void)changeFontSize:(float)newFontSize;一起使用,然后调用self而不是currentTextViewEdit.

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];

这篇关于更改字体大小而无需更改UITextView attributedText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆