Reputation: 2227
My app creates several UITextViews while it's loading , inside the viewDidLoad method, since i need them to be created while the app launches for being able to scroll between views and have them slide with the scrolled view. I tried to create a UIButton and assign it the functionality of changing the font size of the text inside the UITextView. However, since it's programmatically created within the viewDidLoad method i can't get access on them, nothing happens. I tried to create the UITextViews by using the storyboard but didn't get the needed the result so i can't change my creation methods. Also, it's not logical to recall the viewDidLoad method when i press the button since it has lots of methods defined in it and it will drain the CPU power to recall it each time i need to change the font size. Here is how i create the 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];
}
And i tried this method in my Button :
- (IBAction)increaseFontFun:(id)sender {
[leftTextView setFont:[UIFont boldSystemFontOfSize:24.0f]];
}
So nothing happened no result. How can i achieve my purpose ? Many thanks
Upvotes: 0
Views: 408
Reputation: 1559
From the code that you provided, in increaseFontFun:
leftTextView
will have a reference to the last UItextView
that was created in the viewDidLoad
method.
I am not sure if I understand correctly but if you want all the UITextViews' font size to change you should loop through the leftTextFieldsArray
and set the font for each one, or use NSArray's method makeObjectsPerformSelector:withObject:
Here is a sample
for(UITextView * textView in leftTextFieldsArray){
textView.font = [UIFont boldSystemFontOfSize:12];
}
Upvotes: 2
Reputation: 8511
Your code....
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;
}
......
From the NSLog you've put in place the frame for the textview is 0,0,0,0 and therefore can't be seen. You need to add some initialisation that covers the fact that i
might not be one of the values you are expecting
Upvotes: 0
Reputation:
You can do This It's Very Simple:
UITextField *field = [[UITextField alloc] init];
field.font = [UIFont fontWithName:@"ArialMT" size:50.0f];
Upvotes: 0