gpt4 book ai didi

ios - 在我以编程方式实例化的 UITextView(使用 NSTextContainer 初始化)中,.text 属性始终为 nil

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:17:11 26 4
gpt4 key购买 nike

[在底部更新了解决方案和工作代码]

在-viewDidLoad我分配,initWithFrame:

将 myTextView 添加到 subView

设置一些基本属性(对齐方式、背景颜色、文本颜色等)

设置默认文本

.text 没有出现。 myTextView 出现(如背景颜色所示),设置断点,它有框架、内存等。一切看起来都正确。 myTextView 看起来不错,但 .text 为零。我改变它,设置它,更新它。不管怎样,.text 仍然是 nil。

我一遍又一遍地阅读文档。没有提及我没有做的任何事情。我不知所措。帮助。

在@interface MyController()...

一切都曾经是(弱的)但我把它变成了(强)以防万一。没有骰子。

@property (strong, nonatomic) UIScrollView *scrollView;

@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UITextView *contentTextView;

在 viewDidLoad...

- (void)viewDidLoad {

[super viewDidLoad];

// scroll view
CGSize size = CGSizeMake(703, self.view.frame.size.width);
UIScrollView *aScrollView = [[UIScrollView alloc] initWithFrame: self.view.frame];
self.scrollView = aScrollView;
[self.scrollView setDelegate: self];
[self.scrollView setDirectionalLockEnabled: YES];
[self.scrollView setContentSize: size];
[self.view addSubview: self.scrollView];

// image view
CGRect frame = CGRectMake(0, 0, 703, 400);
UIImageView *anImageView = [[UIImageView alloc] initWithFrame: frame];
self.imageView = anImageView;
[self.scrollView addSubview: self.imageView];
[self.imageView setBackgroundColor: [UIColor blueColor]];
[self.imageView setContentMode: UIViewContentModeScaleAspectFit];

// text view
frame = CGRectMake(0, self.imageView.frame.size.height, 703, self.view.frame.size.width - self.imageView.frame.size.height);
size = CGSizeMake(self.view.frame.size.height -320, self.view.frame.size.width - self.imageView.frame.size.height);
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize: size];
UITextView *aTextView = [[UITextView alloc] initWithFrame: frame textContainer: textContainer];
self.contentTextView = aTextView;
[self.scrollView addSubview: self.contentTextView];
self.contentTextView.delegate = self;
self.contentTextView.editable = NO;
self.contentTextView.hidden = NO;
[self.body setBackgroundColor: [UIColor orangeColor]];
// text characteristics
self.contentTextView.textColor = [UIColor blueColor];
self.contentTextView.textAlignment = NSTextAlignmentNatural;
self.contentTextView.font = [UIFont fontWithName: @"Helvetica" size: 30];
self.contentTextView.text = @"SOME AWESOME TEXT";
self.contentTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
// text view specific
self.contentTextView.contentSize = size;

// controller
[self setEdgesForExtendedLayout:UIRectEdgeNone];

}

[更新:解决方案]

当使用 NSTextContainer 分配/初始化 UITextView 时,您还需要单独初始化 NSLayoutManager 和 NSTextStorage 以使 .text 保持不变。

这是更新后的工作代码

NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize: size];
NSLayoutManager *layoutManager = [NSLayoutManager new];
self.layoutManager = layoutManager;
[layoutManager addTextContainer: textContainer];
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString: kBaconIpsum];
self.textStorage = textStorage;
[textStorage addLayoutManager: layoutManager];
UITextView *aTextView = [[UITextView alloc] initWithFrame: frame textContainer: textContainer];
self.contentTextView = aTextView;
[self.scrollView addSubview: self.contentTextView];

最佳答案

NSTextContainer 是iOS 7.0 的新特性,它定义了文本布局的区域。并且根据 Apple 的 Documentations ,它说“新的文本容器必须先添加到 NSLayoutManager 对象才能使用。”。我认为这就是为什么 .text 总是 nil 的原因。

关于ios - 在我以编程方式实例化的 UITextView(使用 NSTextContainer 初始化)中,.text 属性始终为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20412563/

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