1

我想让用户调整 UITextView 的大小。我在 UITextView 的右下角显示一个按钮,例如当用户移动它时,UITextView 跟随它并调整大小。

这工作正常,但文本空间有一个奇怪的行为。在调整大小的过程中,文本空间似乎越来越小。所以如果我调整了很多,先到大框架再到小框架,文本空间变得比框架小。

- (id) init{//My subclass of UITextView
    self = [super init];
    if(self){
        //Resize button
        //Init ResizeButton
        self.resizeButton = [[UIButton alloc]init];

        //Image
        [self.resizeButton setImageForAllState:[UIImage imageNamed:@"Isosceles-right-triangle.png"]];

        //Target
        [self.resizeButton addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
        [self.resizeButton addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragOutside];

        //Add to view
        [self addSubview:resizeButton];

        //Self
        //text font
        [self setFont:[UIFont fontWithName:@"papyrus" size:17]];

    }
    return self;
}

- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{


//  NSLog(@"wasDragged");// get the touch
    UITouch *touch = [[event touchesForView:button] anyObject];

    // get delta
    CGPoint previousLocation = [touch previousLocationInView:button];
    CGPoint location = [touch locationInView:button];
    CGFloat delta_x = location.x - previousLocation.x;
    CGFloat delta_y = location.y - previousLocation.y;

    // move button
    button.center = CGPointMake(button.center.x + delta_x,
                                button.center.y + delta_y);


    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width+ delta_x, self.frame.size.height+ delta_y);

}

视图也是可拖动的

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    // Calculate offset
    CGPoint pt = [[touches anyObject] locationInView:self];
    float dx = pt.x - startLocation.x;
    float dy = pt.y - startLocation.y;
    CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy);

    // Set new location
    self.center = newcenter;

}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // Calculate and store offset, and pop view into front if needed
    CGPoint pt = [[touches anyObject] locationInView:self];
    startLocation = pt;
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {


}

编辑

调整大小之前 调整大小之前

调整大小 调整大小

恢复原尺寸 恢复原尺寸

4

2 回答 2

1

嗯,我也有这个问题。解决方案是继承 UITextView 并覆盖 setFrame: 方法。在设置你想要的框架之前,为 textView 设置 CGRectZero 框架。它将解决您的问题。代码如下。

- (void)setFrame:(CGRect)frame
{
    [super setFrame:CGRectZero];
    [super setFrame:frame];
}
于 2013-05-01T09:20:23.327 回答
0

似乎您可以做以下两件事之一:

第一:关闭控件的“调整到适合”

textfield.adjustsFontSizeToFitWidth = NO;

第二:将字体大小硬编码到代码中

textfield.font = [UIFont fontWithName:@"desired font name" size:10];
于 2013-02-18T13:14:12.497 回答