在我的项目中,我将以编程方式在视图控制器上添加UIScrollview,并在此滚动视图上添加内容视图。
然后我在内容视图上添加了两个文本视图,这里是我的问题:我希望嵌入的文本视图根据它们保持的文本大小进行扩展或缩小。
我似乎正确地扩展了第一个文本视图,但第二个文本视图似乎没有正确扩展,而是在其中裁剪了一些文本。我希望它能在所有设备中普遍使用。
我搜索了很多,但我没有得到解决方案。我的代码如下,请帮助我。
#import "ViewController.h"
@interface ViewController ()
{
UITextView * TextView;
UITextView * TextView1;
UIScrollView * scrollView;
UIView * containerView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView = [[UIScrollView alloc] init];
scrollView.backgroundColor = [UIColor redColor];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:scrollView];
containerView = [[UIView alloc] init];
containerView.backgroundColor = [UIColor whiteColor];
containerView.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:containerView];
NSDictionary * views = NSDictionaryOfVariableBindings(scrollView,containerView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView(600)]|" options:0 metrics:nil views:views]];
TextView = [[UITextView alloc]init];
TextView.translatesAutoresizingMaskIntoConstraints = NO;
TextView.text = @"De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.";
TextView.backgroundColor = [UIColor lightGrayColor];
[containerView addSubview:TextView];
TextView1 = [UITextView new];
TextView1.translatesAutoresizingMaskIntoConstraints = NO;
TextView1.text = @"He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.";
TextView1.backgroundColor = [UIColor lightGrayColor];
[containerView addSubview:TextView1];
//Applying autolayouts for textview1
NSLayoutConstraint * constraint2 = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: containerView attribute:NSLayoutAttributeTop multiplier:1.0f constant:20.0f];
[containerView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:containerView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:5.0f];
[containerView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:containerView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-5.0f];
[containerView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:200.0f];
[containerView addConstraint:constraint2];
//Applying autolayouts for textview2
constraint2 = [NSLayoutConstraint constraintWithItem:TextView1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: TextView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:20.0f];
[containerView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:TextView1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:containerView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:5.0f];
[containerView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:TextView1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:containerView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-5.0f];
[containerView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:TextView1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:200.0f];
[containerView addConstraint:constraint2];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containerView(==scrollView)]|"
options:0
metrics:nil
views:views]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView(==scrollView)]|"
options:0
metrics:nil
views:views]];
}
@end