简体   繁体   English

更改在ViewDidLoad中以编程方式创建的UITextView的字体大小

[英]Chaging the Font size of UITextView created programmatically in ViewDidLoad

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. 我的应用程序在加载时会在viewDidLoad方法内创建多个UITextViews,因为我需要在应用程序启动时创建它们,以便能够在视图之间滚动并使其与滚动视图一起滑动。 I tried to create a UIButton and assign it the functionality of changing the font size of the text inside the UITextView. 我试图创建一个UIButton并为其分配功能来更改UITextView中文本的字体大小。 However, since it's programmatically created within the viewDidLoad method i can't get access on them, nothing happens. 但是,由于它是在viewDidLoad方法中以编程方式创建的,因此无法访问它们,因此什么也没有发生。 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. 我尝试使用情节提要板创建UITextViews,但未获得所需的结果,因此无法更改我的创建方法。 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. 另外,当我按下按钮时调用viewDidLoad方法是不合逻辑的,因为它定义了很多方法,并且每次我需要更改字体大小时,它都会消耗CPU资源来调用它。 Here is how i create the UITextViews: 这是我创建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 : 我在Button中尝试了这种方法:

- (IBAction)increaseFontFun:(id)sender {
    [leftTextView setFont:[UIFont boldSystemFontOfSize:24.0f]];
}

So nothing happened no result. 所以什么也没发生,没有结果。 How can i achieve my purpose ? 我怎样才能达到目的? Many thanks 非常感谢

From the code that you provided, in increaseFontFun: leftTextView will have a reference to the last UItextView that was created in the viewDidLoad method. 从您提供的代码中,在increaseFontFun: leftTextViewincreaseFontFun: leftTextView将引用在viewDidLoad方法中创建的最后一个UItextView

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: 我不确定我是否理解正确,但是如果您想更改所有UITextViews的字体大小,则应遍历leftTextFieldsArray并为每个字体设置字体,或者使用NSArray的方法makeObjectsPerformSelector:withObject:

Here is a sample 这是一个样本

for(UITextView * textView in leftTextFieldsArray){
    textView.font = [UIFont boldSystemFontOfSize:12];
}

You can do This It's Very Simple: 您可以做到这一点,这很简单:

UITextField *field = [[UITextField alloc] init];
field.font = [UIFont fontWithName:@"ArialMT" size:50.0f];

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. 在NSLog中,您已将textview的框架设置为0,0,0,0,因此无法看到。 You need to add some initialisation that covers the fact that i might not be one of the values you are expecting 您需要添加一些初始化,以涵盖以下事实: i可能不是您期望的值之一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM