【问题标题】:Change font size without Change UITextView attributedText更改字体大小而不更改 UITextView 属性文本
【发布时间】:2016-07-04 01:11:34
【问题描述】:

我尝试在 UITextView 中更改字体大小,每次更改属性文本时都会恢复为默认值。

用这个我把中间的测试加粗:

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

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

before:中间是粗体。

之后:所有文字加粗

我希望在大小改变后加粗的留在原地。

编辑:

我尝试了这两种方法:

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

【问题讨论】:

  • 如何更改字体大小?
  • currentTextViewEdit.font = [UIFont fontWithName:currentTextViewEdit.font.fontName size:textSize];
  • 未测试,但:pastebin.com/wFGhK5W1?
  • 如果可行,您能否验证我的回答,以帮助有相同问题的人清楚地看到解决方案而不是发表评论?
  • 你明白了(-:谢谢

标签: objective-c uitextview nsattributedstring


【解决方案1】:

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

关于您的其他解决方案:
粗体效果在NSFontAttributeName 内,它是Font 值。因此,如果您将其替换为通用字体,则会消除“粗体”效果。斜体也一样。

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

您可以使用 UITextView 上的类别和方法 -(void)changeFontSize:(float)newFontSize;,然后调用 self 而不是 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];

【讨论】:

    【解决方案2】:

    不清楚您的问题是什么。但是,您的观察完全正确:如果您使用的是属性文本 (attributedText),那么如果您更改文本视图的纯文本功能(如 font),它会重置属性文本。那是因为你不应该这样做。您不得尝试将属性文本功能与纯文本功能相结合。仅当您使用纯文本 text 时才使用纯文本功能(如 font)。

    例如,如果您想在使用属性文本时更改字体,请更改属性文本的字体。为此,请取出 attributedText,将其放入 NSMutableAttributedString,进行更改,然后将其分配回 attributedText

    【讨论】:

    • 您没有做出任何改变我的回答的任何改变。我支持它。
    猜你喜欢
    • 1970-01-01
    • 2011-09-24
    • 2016-01-02
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 2011-05-21
    • 1970-01-01
    相关资源
    最近更新 更多