我设置了一个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生成了一个单视图应用程序。然后我在Main.storyboard中放入两个UITextViews。一种是使用在UITraitCollection压缩文件上选择的不同文本。

现在,我向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:不会有什么不同。
发布于 2017-06-27 23:38:24
我认为,问题可能在于,在viewDidLoad()中更改文本还为时过早。我猜这些更改在稍后应用size类/变体时会被覆盖。
我可以通过更改viewDidLayoutSubviews()而不是viewDidLoad()中的文本来解决这个问题。
发布于 2017-01-23 23:14:17
您应该使用的方法应该是以下内容的变体:
self.textView.attributedText = [[NSAttributedString alloc]
initWithString:@“xx”
attributes:@{}];https://stackoverflow.com/questions/41809312
复制相似问题