将UILabel添加到UITextView

时间:2016-05-04 15:28:01

标签: ios objective-c uilabel uitextview

我有多个UITextView,我用编程方式创建了for循环。我正在尝试在每个UILabel的左上角添加UITextView。 我怎么能这样做?

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

2 个答案:

答案 0 :(得分:1)

我之前从未向UILabel添加UITextView,但我刚刚对此进行了测试并且有效。这是一般的想法:

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

答案 1 :(得分:1)

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];