UITextView 文本空间在调整大小时变小
Posted
技术标签:
【中文标题】UITextView 文本空间在调整大小时变小【英文标题】:UITextView text space go smaller during rezise 【发布时间】:2013-02-18 13:06:33 【问题描述】:我想让用户调整 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
编辑
调整大小之前
调整大小
回到原来的大小
【问题讨论】:
【参考方案1】:嗯,我也有这个问题。解决方案是继承 UITextView 并覆盖 setFrame: 方法。在设置你想要的框架之前,为 textView 设置 CGRectZero 框架。它将解决您的问题。代码如下。
- (void)setFrame:(CGRect)frame
[super setFrame:CGRectZero];
[super setFrame:frame];
【讨论】:
【参考方案2】:似乎你可以做以下两件事之一:
首先:关闭控件的'Adjust to fit'
textfield.adjustsFontSizeToFitWidth = NO;
第二:将字体大小硬编码到代码中
textfield.font = [UIFont fontWithName:@"desired font name" size:10];
【讨论】:
1. “textfield.adjustsFontSizeToFitWidth = NO;”对于 UITextView ? 2.我是在init方法中做的。以上是关于UITextView 文本空间在调整大小时变小的主要内容,如果未能解决你的问题,请参考以下文章
当 UITextView 大小发生变化时,如何调整 UITableViewCell 的大小?