【问题标题】:UITextView text is loosing font size when i set an Attribute on NSTextStorage with NSUnderlineStyleAttributeName当我使用 NSUnderlineStyleAttributeName 在 NSTextStorage 上设置属性时,UITextView 文本正在丢失字体大小
【发布时间】:2014-04-01 19:30:06
【问题描述】:

在我的应用程序中,我有一个 UItextView,它的输入视图有附件视图,上面有几个按钮。

我正在尝试将已输入的文本更改为下划线文本。它工作正常,但在转换后会丢失字体和字体大小。

这就是我正在做的事情:

NSRange selectedRange = [self.commentsTextView selectedRange];

            NSDictionary *currentAttributesDict = [self.commentsTextView.textStorage attributesAtIndex:selectedRange.location
                                                                            effectiveRange:nil];

            NSDictionary *dict;

            if ([currentAttributesDict objectForKey:NSUnderlineStyleAttributeName] == nil ||
                [[currentAttributesDict objectForKey:NSUnderlineStyleAttributeName] intValue] == 0) {

                dict = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInt:1]};

            }
            else{
                dict = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInt:0]};
            }

            [self.commentsTextView.textStorage beginEditing];
            [self.commentsTextView.textStorage setAttributes:dict range:selectedRange];
            [self.commentsTextView.textStorage endEditing];

【问题讨论】:

    标签: ios objective-c textkit


    【解决方案1】:

    您正在使用只有NSUnderlineStyleAttributeName 值的字典设置该文本范围的属性。

    在您的if... else 结构中,除了NSUnderlineStyleAttributeName 之外,还为dict 提供您想要的字体字体大小 值。

    您之前是如何设置字体字体大小的?

    来自 Apple 的文档 UITextViewOverview 部分:

    在 iOS 6 及更高版本中,该类通过以下方式支持多种文本样式 使用属性文本属性。 (样式文本不支持 iOS 的早期版本。)为此属性设置值会导致 文本视图使用属性中提供的样式信息 细绳。您仍然可以使用字体、textColor 和 textAlignment 属性来设置样式属性,但这些属性适用于所有 文本视图中的文本。

    https://developer.apple.com/library/ios/documentation/uikit/reference/uitextview_class/Reference/UITextView.html

    当您为特定范围的文本设置属性时,它将覆盖该范围内以前存在的任何样式,仅使用新属性。

    【讨论】:

    • 知道了。需要组合的属性。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 2012-02-20
    相关资源
    最近更新 更多