-[UITextView setAttributedText:] 在我使用 UITraitCollection 变体时停止工作
-[UITextView setAttributedText:] stops working, when I use UITraitCollection variations
我将 UITextView 设置为显示不同的属性字符串,具体取决于所使用的设备。但是一旦我使用特征,setText: 就不再起作用了。
我也不知道这两个文本的信息存储在哪里。
当我查看故事板时,我看到:
<textView>
<attributedString key="attributedText">
<fragment content="Large">
<attributes>
<font key="NSFont" metaFont="system" size="14"/>
<paragraphStyle key="NSParagraphStyle" alignment="natural" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0"/>
</attributes>
</fragment>
</attributedString>
<variation key="heightClass=regular-widthClass=compact">
<attributedString key="attributedText">
<fragment content="Compact">
<attributes>
<font key="NSFont" size="12" name=".AppleSystemUIFont"/>
<paragraphStyle key="NSParagraphStyle" alignment="natural" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0"/>
</attributes>
</fragment>
</attributedString>
</variation>
</textView>
但是我如何在运行时访问(和更改)这个变体?
如何重现:
我有最简单的设置。我让 Xcode 为我生成一个单一视图应用程序。然后我将两个 UITextViews
放入 Main.storyboard
。一种是使用在 UITraitCollection
compact 上选择的不同文本。
现在我向 ViewController
添加了两个出口,并在 viewDidLoad
中编写了一些代码来更改内容。
@interface ViewController : UIViewController
@property( assign, nonatomic) IBOutlet UITextView *textView; // traits
@property( assign, nonatomic) IBOutlet UITextView *textView2; // no traits
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSParameterAssert( [self.textView isKindOfClass:[UITextView class]]);
NSParameterAssert( [self.textView2 isKindOfClass:[UITextView class]]);
NSParameterAssert( self.textView != self.textView2);
[self.textView setText:@"XXXX"]; // does not show up
[self.textView2 setText:@"XXXX"]; // works as expected
}
使用 -setAttributedText:
没有区别。
你应该使用的方法应该是这个的变体:
self.textView.attributedText = [[NSAttributedString alloc]
initWithString:@“xx”
attributes:@{}];
我认为,问题可能在于更改 viewDidLoad() 中的文本还为时过早。我想当应用大小 类 / 变体时,这些更改会在稍后的某个时候被覆盖。
我可以通过更改 viewDidLayoutSubviews() 而不是 viewDidLoad() 中的文本来解决这个问题。
我将 UITextView 设置为显示不同的属性字符串,具体取决于所使用的设备。但是一旦我使用特征,setText: 就不再起作用了。
我也不知道这两个文本的信息存储在哪里。 当我查看故事板时,我看到:
<textView>
<attributedString key="attributedText">
<fragment content="Large">
<attributes>
<font key="NSFont" metaFont="system" size="14"/>
<paragraphStyle key="NSParagraphStyle" alignment="natural" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0"/>
</attributes>
</fragment>
</attributedString>
<variation key="heightClass=regular-widthClass=compact">
<attributedString key="attributedText">
<fragment content="Compact">
<attributes>
<font key="NSFont" size="12" name=".AppleSystemUIFont"/>
<paragraphStyle key="NSParagraphStyle" alignment="natural" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0"/>
</attributes>
</fragment>
</attributedString>
</variation>
</textView>
但是我如何在运行时访问(和更改)这个变体?
如何重现:
我有最简单的设置。我让 Xcode 为我生成一个单一视图应用程序。然后我将两个 UITextViews
放入 Main.storyboard
。一种是使用在 UITraitCollection
compact 上选择的不同文本。
现在我向 ViewController
添加了两个出口,并在 viewDidLoad
中编写了一些代码来更改内容。
@interface ViewController : UIViewController
@property( assign, nonatomic) IBOutlet UITextView *textView; // traits
@property( assign, nonatomic) IBOutlet UITextView *textView2; // no traits
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSParameterAssert( [self.textView isKindOfClass:[UITextView class]]);
NSParameterAssert( [self.textView2 isKindOfClass:[UITextView class]]);
NSParameterAssert( self.textView != self.textView2);
[self.textView setText:@"XXXX"]; // does not show up
[self.textView2 setText:@"XXXX"]; // works as expected
}
使用 -setAttributedText:
没有区别。
你应该使用的方法应该是这个的变体:
self.textView.attributedText = [[NSAttributedString alloc]
initWithString:@“xx”
attributes:@{}];
我认为,问题可能在于更改 viewDidLoad() 中的文本还为时过早。我想当应用大小 类 / 变体时,这些更改会在稍后的某个时候被覆盖。
我可以通过更改 viewDidLayoutSubviews() 而不是 viewDidLoad() 中的文本来解决这个问题。