我的应用程序在viewDidLoad方法中加载时会创建几个UITextView,因为我需要在应用程序启动时创建它们,以便能够在视图之间滚动并让它们随滚动视图一起滑动。我尝试创建一个UIButton,并为其分配更改UITextView内文本字体大小的功能。但是,由于它是在viewDidLoad方法中以编程方式创建的,因此无法访问它们,因此没有任何反应。我试图通过使用故事板创建UITextViews,但没有得到所需的结果,所以我不能改变我的创建方法。此外,当我按下按钮时调用viewDidLoad方法是不合逻辑的,因为它中定义了许多方法,每次我需要更改字体大小时,它会耗尽CPU功率来调用它。以下是我创建UITextViews的方法:
- (void)viewDidLoad {
CGSize imageSize;
imageSize.height = 693;
imageSize.width = 512;
self.scroll1.minimumZoomScale = 1.0;
self.scroll1.maximumZoomScale = 1.0;
self.scroll1.frame = CGRectMake(0,0,imageSize.width,imageSize.height);
self.scroll1.scrollEnabled = YES;
self.scroll1.contentSize = CGSizeMake(imageSize.width * imgs.count, imageSize.height );
self.scroll1.pagingEnabled = YES;
self.scroll1.bounces = false;
self.scroll1.delegate = self;
leftTextFieldsArray=[NSMutableArray array];
rightTextFieldsArray=[NSMutableArray array];
int counter = 0 ;
for (int i = 0; i <= pants - 2; i ++)
{
CGFloat x = counter * 1024;
subView = [[UIScrollView alloc]initWithFrame:CGRectMake(x, 0, 1024, 693)];
subView.clipsToBounds = NO;
UIImage *img;
UIImage *img2;
img = [imgs objectAtIndex:i];
img2 = [imgs objectAtIndex:i+1];
imageView = [[UIImageView alloc ] initWithImage:img];
imageView.frame = CGRectMake(i * 512, 0, 512, 693);
imageView2 = [[UIImageView alloc ] initWithImage:img2];
imageView2.frame = CGRectMake((i+1) * 512, 0, 512, 693);
///////////// ///////////// ///////////// TextViews Created///////////// /////////////
rightTextView = [[UITextView alloc]init];
leftTextView = [[UITextView alloc]init];
rightTextView.scrollEnabled = YES;
leftTextView.scrollEnabled = YES;
rightTextView.editable = NO;
leftTextView.editable = NO;
//////////////////////////////Specific Behavior regarding positioning in each view//////////
if(i == 4) {
leftTextView.frame = CGRectMake(2048, 0, 500,300);
}
else if(i == 6){
leftTextView.frame = CGRectMake(3072, 450, 500,300);
rightTextView.frame = CGRectMake(3584, 0, 500,200);
rightTextView.layer.zPosition = 2;
}
else if(i==8){
leftTextView.frame = CGRectMake(4096, 0, 500,300);
rightTextView.frame = CGRectMake(4608, 450, 500,300);
rightTextView.layer.zPosition = 2;
}
else if(i==10){
leftTextView.frame = CGRectMake(5120, 0, 500,300);
rightTextView.frame = CGRectMake(5632, 0, 500,300);
rightTextView.layer.zPosition = 2;
}
else if(i==12){
leftTextView.frame = CGRectMake(6144, 0, 500,300);
rightTextView.frame = CGRectMake(6656, 450, 500,300);
rightTextView.layer.zPosition = 2;
}
else if(i==14) {
leftTextView.frame = CGRectMake(7168, 450, 500,300);
rightTextView.frame = CGRectMake(7680, 0, 500,300);
rightTextView.layer.zPosition = 2;
}
else if(i==16) {
leftTextView.frame = CGRectMake(8192, 0, 500,300);
rightTextView.frame = CGRectMake(8674, 0, 500,300);
rightTextView.layer.zPosition = 2;
}
else if(i==18){
leftTextView.frame = CGRectMake(9216, 450, 500,300);
rightTextView.frame = CGRectMake(9728, 0, 500,300);
rightTextView.layer.zPosition = 2;
}
else if(i==20){
leftTextView.frame = CGRectMake(10240, 450, 500,300);
rightTextView.frame = CGRectMake(10752, 450, 500,300);
rightTextView.layer.zPosition = 2;
}
else if(i==22){
leftTextView.frame = CGRectMake(11264, 0, 500,300);
rightTextView.frame = CGRectMake(11776, 0, 500,300);
rightTextView.layer.zPosition = 2;
}
rightTextView.backgroundColor = [UIColor clearColor];
rightTextView.textColor = [UIColor blackColor];
rightTextView.text = rightArray[i/2];
[rightTextFieldsArray addObject:rightTextView];
leftTextView.backgroundColor = [UIColor clearColor];
leftTextView.textColor = [UIColor blackColor];
leftTextView.text = leftArray[i/2];
[leftTextFieldsArray addObject:leftTextView];
subView.scrollEnabled = NO;
subView.userInteractionEnabled = NO;
[subView addSubview:imageView];
[imageView2 removeFromSuperview];
[subView addSubview:leftTextView];
[self.scroll1 addSubview:rightTextView];
[self.scroll1 addSubview:subView];
}
counter ++;
[super viewDidLoad];
}
我在Button中尝试了这个方法:
- (IBAction)increaseFontFun:(id)sender {
[leftTextView setFont:[UIFont boldSystemFontOfSize:24.0f]];
}
所以没有结果没有结果。我怎样才能达到目的?非常感谢
答案 0 :(得分:2)
从您提供的代码中,increaseFontFun:
leftTextView
将引用UItextView
方法中创建的最后一个viewDidLoad
。
我不确定我是否理解正确,但是如果你想要改变所有UITextViews的字体大小,你应该遍历leftTextFieldsArray
并为每个字体设置字体,或者使用NSArray的方法makeObjectsPerformSelector:withObject:
这是一个示例
for(UITextView * textView in leftTextFieldsArray){
textView.font = [UIFont boldSystemFontOfSize:12];
}
答案 1 :(得分:0)
你可以做到这很简单:
UITextField *field = [[UITextField alloc] init];
field.font = [UIFont fontWithName:@"ArialMT" size:50.0f];
答案 2 :(得分:0)
你的代码......
leftTextView = [[UITextView alloc]init];
rightTextView.scrollEnabled = YES;
leftTextView.scrollEnabled = YES;
rightTextView.editable = NO;
leftTextView.editable = NO;
if(i == 4) {
leftTextView.frame = CGRectMake(2048, 0, 500,300);
}
else if(i == 6){
leftTextView.frame = CGRectMake(3072, 450, 500,300);
rightTextView.frame = CGRectMake(3584, 0, 500,200);
rightTextView.layer.zPosition = 2;
}
......
从NSLog中你已经设置了textview的框架是0,0,0,0因此无法看到。您需要添加一些初始化,其中包含i
可能不是您期望的值之一的事实