简体   繁体   English

当我使用NSUnderlineStyleAttributeName在NSTextStorage上设置属性时,UITextView文本丢失了字体大小

[英]UITextView text is loosing font size when i set an Attribute on NSTextStorage with NSUnderlineStyleAttributeName

In my App i have a UItextView and its input view has accessory view with couple of buttons on it. 在我的应用程序中,我有一个UItextView,它的输入视图有附件视图,上面有几个按钮。

I am trying to change the text already being typed to underline text. 我正在尝试将已输入的文本更改为下划线文本。 Its working fine but its loosing the font and the font size after conversion. 它工作正常,但转换后失去了字体和字体大小。

Here's what i am doing: 这就是我在做的事情:

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

You're setting the attributes for that range of text using a dictionary that only has a value for NSUnderlineStyleAttributeName . 您正在使用仅具有NSUnderlineStyleAttributeName值的字典设置该文本范围的属性。

In your if... else structure, in addition to NSUnderlineStyleAttributeName give dict the values that you want for the font and font size . if... else结构中,除了NSUnderlineStyleAttributeName之外, NSUnderlineStyleAttributeName字体字体大小提供dict所需的值。

How did you previously set the font and font size ? 你之前是如何设置字体字体大小的

From Apple's doc on UITextView the Overview section: 从Apple的UITextView文档概述部分:

In iOS 6 and later, this class supports multiple text styles through use of the attributedText property. 在iOS 6及更高版本中,此类通过使用attributedText属性支持多种文本样式。 (Styled text is not supported in earlier versions of iOS.) Setting a value for this property causes the text view to use the style information provided in the attributed string. (早期版本的iOS不支持样式文本。)为此属性设置值会导致文本视图使用属性字符串中提供的样式信息。 You can still use the font, textColor, and textAlignment properties to set style attributes, but those properties apply to all of the text in the text view. 您仍然可以使用font,textColor和textAlignment属性来设置样式属性,但这些属性适用于文本视图中的所有文本。

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

When you set the attributes for a specific range of text, it will overwrite any styles that previously existed in that range, using the new attributes only. 为特定范围的文本设置属性时,它将仅使用新属性覆盖以前存在于该范围内的所有样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM