简体   繁体   中英

Display UILabel in UIView programatically or using Storyboard

I have added a UIView and now, i want to show a UILabel . However, it does not get displayed. Can someone help me out please ?

- (IBAction)infoButtonClicked:(id)sender {
    myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);


    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(5, 10, self.view.frame.size.width, 100)];

    [myView addSubview:label];

    [self.view addSubview: myView];

    [UIView animateWithDuration:0.3/1.5 animations:^{
        myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3/2 animations:^{
            myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3/2 animations:^{
                myView.transform = CGAffineTransformIdentity;

                label.text=@"The title";

            }];
        }];
    }];

    //The setup code to detect single touch
    UITapGestureRecognizer *singleFingerTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(oneTap:)];
    [myView addGestureRecognizer:singleFingerTap];



}

try this

UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(5, 10, myView.frame.size.width, 100)];

label.text = @"sample TExt";

label.numberOfLines = 1;
label.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
label.adjustsFontSizeToFitWidth = YES;
label.adjustsLetterSpacingToFitWidth = YES;
label.minimumScaleFactor = 10.0f/12.0f;
label.clipsToBounds = YES;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentLeft;

[myView addSubview:label];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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