因为最近想要在 UIScrollView 里面放大量文本 + 一些图片(UIImageView)。所以我添加了一个 UITextView 到 UIScrollView 里面,禁止 UITextView 的 Scroll 同时用下面的让其高度自动适应其中内容文本的高度。
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
代码放在 .m 文件的 didload 函数里面。貌似 didload 函数被执行的时候 UITextView 并没有被载入,可能是因为其在 UIScrollView 内的关系。所以这段代码如果要起到效果,就必须不能用 Storyboard 来添加 UITextView,必须手工用代码 addSubView,然后在其后面添加这段代码;或者用按键事件触发(仅作测试)。
接下来碰到了超奇怪的问题。UITextView 的确高度自动适应了,但是仅仅在刚开始(用按键时间触发后)是正常的,但是只要滚动 UIScrollView,UITextView 就会立即变回原先的高度。还有一个奇怪的问题就是 UITextView 高度自动适应时,内部的内容会被向上移动一段距离,就是说顶部有几行会看不见掉,但是整体高度的确是增加了,也可以看到结尾的文本了。
求教,这是神马状况?! >~<。
还是说把 UITextView 放到 UIScrollView 里面的思路一开始就错了?
代码如下:
#import <UIKit/UIKit.h>
@interface EXFifthViewController : UIViewController
{
}
- (IBAction)buttonTest:(id)sender;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITextView *textView;
- (void)resizeTextView;
@end
#import "EXFifthViewController.h"
@interface EXFifthViewController ()
@end
@implementation EXFifthViewController
@synthesize scrollView;
@synthesize textView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[scrollView setContentInset:UIEdgeInsetsMake(0, 0, 800, 0)];
[self resizeTextView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)resizeTextView
{
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
[textView setFrame:frame];
}
- (IBAction)buttonTest:(id)sender
{
[self resizeTextView];
}
@end
1、
正则,RegexKitLite是用的最多的,DP4用不了考虑降级到4.3||尝试修复,修好发pull request||向作者提feature request试试看,虽然此框架两年没更新了。
还有内建的正则实现,我来一段检查字符串是否是合法的中国手机号的正则匹配:
- (BOOL)isValidChinesePhoneNumber
{
NSString *Regex =@"(13[0-9]|14[57]|15[012356789]|18[02356789])\\d{8}";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
return [predicate evaluateWithObject:self];
}
2、
_textView 后来我改成 textView 了,和 @property 对应好的。
重构改名两个都会被改(xcode的bug)
同样的问题存在于:
//重构一个方法,另一个也会跟着改名,xcode的bug
- (void)test;
+ (void)test;
3、
我修改过的类如下,看下是否满足需求:
.h
#import <UIKit/UIKit.h>
@interface TestViewController : UIViewController<UITextViewDelegate>
@end
.m:
#import "TestViewController.h"
@interface TestViewController ()
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UITextView *textView;
@end
@implementation TestViewController
@synthesize scrollView = _scrollView, textView = _textView;
- (void)viewDidLoad
{
[super viewDidLoad]; // Do any additional setup after loading the view.
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
self.scrollView.backgroundColor = [UIColor greenColor];
self.scrollView.contentInset = UIEdgeInsetsMake(1, 0, 0, 0);
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
self.textView.scrollEnabled = NO;
self.textView.delegate = self;
[self.scrollView addSubview:self.textView];
[self.view addSubview:self.scrollView];
[self resizeTextView];
}
- (void)resizeTextView
{
CGRect frame = self.textView.frame;
frame.size.height = self.textView.contentSize.height;
[self.textView setFrame:frame];
[self.scrollView setContentSize:frame.size];
if (frame.size.height > self.scrollView.frame.size.height)
{
[self.scrollView setContentOffset:CGPointMake(0, frame.size.height - self.scrollView.frame.size.height) animated:YES];
}
}
- (void)textViewDidChange:(UITextView *)textView
{
[self resizeTextView];
}
@end
5.0下测试,4.3可能会有刚进入编辑状态顶部漂移问题,楼主先试试看
UITextView中也有一个UIScrollView,和你的背景UIScrollView是重合的,你的滚动动作会出发两个UIScrollView。
所以,如果你不需要编辑这些文字,建议你用UILabel作为文本容器,然后用sizeToFit来控制高度。
https://github.com/gaosboy/Kache/
这个Demo中是一个内容动态改变的UILabel,高度自适应,放在一个UIScrollView中可以滚动。可以参考