将 UILabel 添加到 UITextView

Add UILabel to UITextView

我有多个 UITextView 是我使用 for 循环以编程方式创建的。我也在尝试在每个 UITextView 的左上角添加一个 UILabel。 我该怎么做?

我的UITextView代码:

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);
}

我以前从未将 UILabel 添加到 UITextView,但我只是测试了这个并且它起作用了。这是总体思路:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
label.text = "My Label"
textView.addSubview(label)

Objective-c代码:-

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