gpt4 book ai didi

ios - -[当我使用 UITraitCollection 变体时,UITextView setAttributedText :] stops working,

转载 作者:行者123 更新时间:2023-11-29 11:49:29 24 4
gpt4 key购买 nike

我有一个 UITextView 设置为显示不同的属性字符串,具体取决于所使用的设备。但是,一旦我使用特征,setText: 就不再起作用了。

我也不知道这两个文本的信息存储在哪里。当我查看 Storyboard时,我看到:

<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 为我生成一个单 View 应用程序。然后我将两个 UITextViews 放入 Main.storyboard 中。一种是使用在 UITraitCollection compact 上选择的不同文本。

Xcode Screeny

现在我向 ViewController 添加了两个 outlet,并在 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: 没有任何区别。

Download Xcode project

最佳答案

我认为,问题可能在于更改 viewDidLoad() 中的文本还为时过早。我想当应用尺码等级/变化时,这些更改会在稍后的某个时候被覆盖。

我可以通过更改 viewDidLayoutSubviews() 而不是 viewDidLoad() 中的文本来解决这个问题。

关于ios - -[当我使用 UITraitCollection 变体时,UITextView setAttributedText :] stops working,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41809312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com