iphone - 自定义 uiscrollview 不滚动(使用 coretext)

标签 iphone objective-c ios ipad uiscrollview

我正在使用 CoreText 在多列中绘制文本(取决于 iPad 的方向)。

为了测试,我创建了一个由数字 100 - 999 组成的 NSMutableString。此文本跨越 5 列,其中 1 或 2 列显示在屏幕上(取决于方向)。

在我的主 ViewController 中,我添加了一个自定义的 UIScrollView 来保存这个文本,我希望它可以滚动。

我注意到 ScrollView 在我设置之前不会滚动:

[myScrollView setContentMode:UIViewContentModeRedraw];

我确实希望 iPad 旋转时 scrollView 调用 drawRect(以调整列数)!

不过,我的问题是它似乎一遍又一遍地调用 drawRect ... 在滚动时(因此分配越来越多的内存,也导致一些滞后)。

我将 UIScrollView 添加到我的主 viewController 中,如下所示:

myScrollView = [[CoreTextTestUIView alloc] init];
myScrollView.parentView = self;
if(FACING == @"PU" || FACING == @"PD")
{
    myScrollView.frame = CGRectMake(0,50,768,974);
}
else
{
    myScrollView.frame = CGRectMake(0,50,1024,718);
}
[myScrollView setContentMode:UIViewContentModeRedraw];
[container addSubview:myScrollView];

同样,我希望在 iPad 旋转时调用 drawRect,因此列数可以改变......但是我不希望它在我只是尝试滚动 UIScrollView 时调用 drawRect。

有人可以帮帮我吗?

...

下面是我的 UIScrollView 的 .m:

#import "CoreTextTestUIView.h"
#import <CoreText/CoreText.h>

@implementation CoreTextTestUIView

@synthesize parentView;


NSMutableString *testText;



-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        // Initialization code

        //set BG color
        self.backgroundColor = [[UIColor alloc] initWithRed:134 green:166 blue:228 alpha:1.0];

        //UIScrollView Stuff
        //self.delegate = self;
        self.scrollEnabled = YES;
        self.pagingEnabled = YES;
        self.userInteractionEnabled = YES;
        [self becomeFirstResponder];
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.bounces = NO;
        self.alwaysBounceHorizontal = YES;
        self.alwaysBounceVertical = NO;

        //generate long text
        testText = [[NSMutableString alloc] initWithString:@""];
        for(int i = 100; i < 1000; i++)
        {
            [testText appendString:[NSString stringWithFormat:@"%i ",i]];
        }

        self.alpha = 0.0;
        [self fadeIn];
    }
    return self;
}



-(void)fadeIn
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];  
    [UIView setAnimationDelegate:self]; 
    //[UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];  
    self.alpha = 1.0;
    [UIView commitAnimations];
}



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
-(void)drawRect:(CGRect)rect
{

    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",testText]];

    //set font
    CTFontRef helvetica = CTFontCreateWithName(CFSTR("Helvetica"), 40.0, NULL);
    [string addAttribute:(id)kCTFontAttributeName
                   value:(id)helvetica
                   range:NSMakeRange(0, [string length])];


    //layout master
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);

    //flip the coordinate system
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    int textPos = 0;
    int columnIndex = 0;

    //how many columns? (orientation dependent)
    float howManyColumns;
    if(parentView.FACING == @"PU" || parentView.FACING == @"PD")
    {
        howManyColumns = 1.0;
    }
    else
    {
        howManyColumns = 2.0;
    }

    //create columns in loop
    while(textPos < [string length])
    {
        NSLog(@"column started");

        //column form
        CGMutablePathRef columnPath = CGPathCreateMutable();
        CGPathAddRect(columnPath, NULL, 
                      CGRectMake((self.bounds.size.width/howManyColumns*columnIndex), 0, 
                                 (self.bounds.size.width/howManyColumns),
                                 self.bounds.size.height));

        //column frame
        CTFrameRef columnFrame = CTFramesetterCreateFrame(framesetter, 
                                                             CFRangeMake(textPos, 0),
                                                             columnPath,
                                                             NULL);

        //use the column path
        CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(textPos, 0), columnPath, NULL);
        CFRange frameRange = CTFrameGetVisibleStringRange(frame);

        //draw
        CTFrameDraw(columnFrame, context);

        //cleanup
        CFRelease(columnFrame);
        CGPathRelease(columnPath);

        textPos += frameRange.length;
        columnIndex++;
    }

    //set scrollView content size
    int totalPages = (columnIndex+1)/howManyColumns;
    self.contentSize = CGSizeMake(totalPages*self.bounds.size.width, self.frame.size.height);

    //release
    CFRelease(framesetter);
    [string release];
}



-(void)dealloc
{
    [super dealloc];

    [parentView release];
    [testText release];
}


@end

最佳答案

看着这个,我看不到你在哪里设置你的 ScrollView 的 contentSize。如果您没有设置 scrollView 的 contentSize,则不会启用滚动,您只会看到适合 scrollView 当前区域的内容。此外,如果您的文本在配置中是静态的,请考虑优化正在发生的一些重绘并将其添加到 ScrollView 的 subview 中。

关于iphone - 自定义 uiscrollview 不滚动(使用 coretext),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10705573/

相关文章:

iphone - lowercaseStringWithLocale是做什么的?

iphone - 在类方法中访问全局声明的 NSArray 值时出错

ios - 按索引移动数组中的元素

objective-c - 为什么不鼓励使用伞式框架?

iphone - 显示别名游戏中心回合制游戏

ios - 在 WkWebView 中找出导航目标

iPhone : How to achieve functionality similar to popToRootViewController using modalView?

iphone - 如何防止传递给 superview 的点击事件?

iphone - Objective-C : call super class

ios - UIPickerView 无法显示 JSON 数组数据