[英]Text garbled when using NSTextContainer/NSLayoutManager with UITextView
[英]In my programmatically instantiated UITextView (initialized with NSTextContainer) the .text property is always nil
[底部有解决方案和工作代码更新]
在-viewDidLoad我分配,initWithFrame:
将myTextView添加到subView
设置一些基本属性(对齐方式,背景颜色,文本颜色等)
设置默认文字
.text不出现。 myTextView出现(如背景颜色所示),设置断点,具有框架,内存等。一切看起来都正确。 myTextView看起来不错,但.text为零。 我对其进行更改,设置,更新。 无论.text保持为零。
我已经读了一遍又一遍的文档。 没有提及我没有做的任何事情。 我很茫然。 救命。
在@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 ...
我将其注释掉,仅使用一个框架,现在显示的是文本,它将显示为.text不再为零。 这就提出了一个问题...应该如何将NSTextContainers与UITextViews一起使用?
// NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize: size];
// UITextView *aTextView = [[UITextView alloc] initWithFrame: frame textContainer: textContainer];
UITextView *aTextView = [[UITextView alloc] initWithFrame: frame];
self.contentTextView = aTextView;
NSTextContainer
是iOS 7.0的新功能,它定义了文本放置的区域。 根据Apple的文档 ,它说:“新文本容器必须先添加到NSLayoutManager对象中,然后才能使用。” 我认为这就是为什么.text
始终为nil
。
这个问题有点老了,但是它帮助我解决了一个类似的问题,试图在scrollView中设置文本格式,并导致我进入图1。创建和配置非视图文本对象 。 物有所值 ...
-(id)makeHelpPage1:(CGRect)rect
{
page1 = [[UIView alloc] initWithFrame:rect];
// NSString *help = [scrollHelp objectAtIndex:1];
NSString *help = @"SOME AWESOME TEXT";
HelpTextView *showHelp = [[HelpTextView alloc] formatHelpTextNew:(NSString*)help];
[page1 addSubview:showHelp];
return page1;
}
#import "HelpTextView.h"
@implementation HelpTextView
-(id)formatText:(NSString*)messageID
{
// TextKit - non-view
NSTextStorage *textStorage;
NSLayoutManager *layoutManager;
NSTextContainer *textContainer;
textStorage = [[NSTextStorage alloc] initWithString:(NSString*)messageID];
layoutManager = [[NSLayoutManager alloc] init];
textContainer = [[NSTextContainer alloc] init];
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
// TextKit - as viewed
UITextView *textView = [[UITextView alloc] initWithFrame: textFrame textContainer: textContainer];
return textView;
}
免责声明:文本未格式化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.