本文介绍了如何使CATextLayer对象像UITextView一样可滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用CATextLayer一起显示粗体和普通文本。现在有一段时间我从Web服务响应中获取了较长的文本,因此我无法显示完整的文本。任何人都可以帮助我,应该怎么做?使CATextLayer对象像textview一样可滚动。请向我建议任何解决方案。我也从进行了引用但这对我不起作用。我正在使用此代码:

I have used CATextLayer for Displaying bold and Normal text together.Now some time i am getting long text from the Web Service response so i am not able to display complete text.Can any one please help me what should be the way to make CATextLayer object Scrollable like textview.Please suggest me any solution regarding this.I have also take reference from Here but it's not working for me. I am using this code:

/ *按需创建文本层* /

/* Create the text layer on demand */

if (!_textLayer) {
    _textLayer = [[CATextLayer alloc] init];
    //_textLayer.font = [UIFont boldSystemFontOfSize:13].fontName; // not needed since `string` property will be an NSAttributedString
    _textLayer.backgroundColor = [UIColor clearColor].CGColor;
    _textLayer.wrapped = YES;
    //CALayer *layer = self.navigationController.toolbar.layer; //self is a view controller contained by a navigation controller
    _textLayer.frame = CGRectMake(5,325, 310, 90);
    _textLayer.contentsScale = [[UIScreen mainScreen] scale]; // looks nice in retina displays too :)
    _textLayer.alignmentMode = kCAAlignmentJustified;
    _textLayer.masksToBounds = YES;
    [self.view.layer addSublayer:_textLayer];
}

在底部,我也尝试将_textLayer添加到UITextView的图层中的self.view.layer。

At bottom I have also tried to add my _textLayer to UITextView's layer as well instead of self.view.layer.

请建议我如何实现此功能?

Please suggest How could i achieve this feature?

推荐答案

您只需将CATextLayer添加到UIScrollView即可免费滚动。
根据需要设置scrollview的框架,并将contentSize设置为CATextLayer框架,并将其添加到滚动的图层中,如下所示(实际上不是工作代码):

You can just add your CATextLayer to UIScrollView and get scrolling for free.Set scrollview's frame as needed and contentSize as your CATextLayer frame and add it to scroll's layer, something like this (not actually working code):

UIScrollView* scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,100,100)];
CATextLayer* layer = [CATextLayer layer];
//layer init code goes here
scroll.contentSize = CGSizeMake(1000,1000);//or your actual text layer size
[scroll.layer addSublayer:layer];

就可以了!不要忘记将滚动添加为视图子视图。

and you are done! Don't forget to add scroll as your view subview.

这篇关于如何使CATextLayer对象像UITextView一样可滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 18:32