我正在尝试将我的 CATextLayer 框架添加到 UIScrollView 以获得一些滚动。我一直在尝试使用此处提到的技术(如何使我的 CATextLayer 对象像 UITextView 一样可滚动?)但没有成功。
实际上,由于(建议的)3 行我已添加到我的代码中,我的 CATextLayer 不再显示。
我在下面附上了我的代码并注意到了这 3 行。也许有人可以帮我解决这个问题,甚至提出一个更好的方法来解决这个问题:-)
// Create the scrool view (FIRST LINE ADDED)
UIScrollView* scrollLayer = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 500.0, 500.0)];
// Create the new layer object
boxLayer = [[CATextLayer alloc] init];
// Give it a size
[boxLayer setBounds:CGRectMake(0.0, 0.0, 500.0, 500.0)];
// Give it a location
[boxLayer setPosition:CGPointMake(300.0, 350.0)];
// Make half-transparent red the background color for the layer
UIColor *reddish = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.1];
// Get CGColor object with the same color values
CGColorRef cgReddish = [reddish CGColor];
[boxLayer setBackgroundColor:cgReddish];
// Make it a sublayer on the view's layer
[self.view.layer addSublayer:boxLayer];
// Create string
NSString *text2 = @"The article was about employment.\nHe leafed through it in an instant.\nHis feeling of anxiety resurfaced and he closed the magazine.\n\n-Hm…, he breathed.\n\n-Have you been looking for work long?, asked the stranger at his side.\nThe article was about employment.\nHe leafed through it in an instant.\nHis feeling of anxiety resurfaced and he closed the magazine.\n\n-Hm…, he breathed.\n\n-Have you been looking for work long?, asked the stranger at his side.\nThe article was about employment.\nHe leafed through it in an instant.\nHis feeling of anxiety resurfaced and he closed the magazine.\n\n-Hm…, he breathed.\n\n-Have you been looking for work long?, asked the stranger at his side.";
// Set font type
[boxLayer setFont:@"MarkerFelt-Thin"];
// Set font size
[boxLayer setFontSize:20.0];
// Text is left justified
[boxLayer setAlignmentMode:kCAAlignmentLeft];
// Text is wrapped
boxLayer.wrapped = YES;
// Assign string to layer
[boxLayer setString:text2];
// Define the text size (SECOND LINE ADDED)
scrollLayer.contentSize = CGSizeMake(500, 1000);
// Set the text layer as a sub layer of the scroll view (THIRD LINE ADDED)
[scrollLayer.layer addSublayer:boxLayer];