gpt4 book ai didi

ios - UITextView 文本空间在调整大小时变小

转载 作者:行者123 更新时间:2023-11-29 13:18:42 24 4
gpt4 key购买 nike

我想让用户调整 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);

}

View 也是可拖动的

-(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 {


}

编辑

调整大小之前 Before resizing

调整大小 Resizing

回到原来的大小 Back to original size

最佳答案

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

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

关于ios - UITextView 文本空间在调整大小时变小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14937085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com