3

I have Multiple UITextView's that I have created with a for-loop programmatically. I'm trying to add a UILabel to the upper left corner of each UITextView also. How can I do that?

My UITextView code:

for (int i = 0; i < 10; i++){
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, yPos, 375,height)];
    [textView setBackgroundColor:[UIColor lightGrayColor]]; //set different property like this
    UIColor *borderColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1.0];
    textView.layer.borderColor = borderColor.CGColor;
    textView.layer.borderWidth = 1.0;
    textView.layer.cornerRadius = 5.0;
    textView.textAlignment=NSTextAlignmentRight;
    textView.editable=NO;

    [CommentScrooll addSubview:textView ];
    // CommentScroll Is the name of my viewcontroller
    yPos += (height + padding);
}

2 Answers 2

2

Objective-c code:-

UILabel *cust_Label = [[UILabel alloc] initWithFrame:CGRectMake(Your_X, Your_Y, Your_Width,Your_Height)];
cust_Label.text=@"Your Text";
[textView addSubview: cust_Label];
1

I've never added a UILabel to a UITextView before but I just tested this and it worked. This is the general idea:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
label.text = "My Label"
textView.addSubview(label)
1
  • ca you write it in Objective-c please?
    – A.M
    May 4, 2016 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.