2013-02-18 38 views
1

我想讓用戶調整UITextView的大小。我在UITextView的底部角落顯示一個按鈕,例如當用戶移動它時,UITextView跟隨它並調整大小。rezise期間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 { 


} 

EDIT

之前調整 Before resizing

調整大小 Resizing

返回到原來的大小 Back to original size

回答

1

嗯,我也有這個問題。解決方法是繼承UITextView並覆蓋setFrame:方法。在設置你想要的框架之前,爲textView設置CGRectZero框架。它會解決你的問題。代碼如下。

- (void)setFrame:(CGRect)frame 
{ 
    [super setFrame:CGRectZero]; 
    [super setFrame:frame]; 
} 
+0

幹得好! Thx你,解決問題。 – Alexandre 2013-05-06 12:30:17

0

好像你可以做兩兩件事之一:

第一:關閉「調整,以適應」控制

textfield.adjustsFontSizeToFitWidth = NO; 

第二:硬編碼字體大小放入代碼

textfield.font = [UIFont fontWithName:@"desired font name" size:10]; 
+0

1.「textfield.adjustsFontSizeToFitWidth = NO;」爲UITextView? 2.我在init方法中這樣做。 – Alexandre 2013-02-18 13:50:27