How to correct set position of UIView from other UiView

| | August 11, 2015

I have custom UITableViewCell xib file, and there I have an UITextView, and some labels that has autolayout.

In my UITableViewController I want add to UITableViewCell new UIViews that I create by the count of “tags”.

The Question : How to add this “tags” correct to UITableViewCell line by line and under the TextView? What is more, this “tags” should add by they width that one tag stay on first line if it width not more than cell width, else it should stay on second line.
This is my code to set position of UIViews for tag on UITableViewCell:

int yPos = cell.TextLabel.frame.size.height;
int xPos = 10;
for (int count = 0; count < Tags.count; count++) {

        UITextView *textView = [[UITextView alloc]init];
        textView.text = Tags[count][@"name"];
        [cell.viewForBaselineLayout addSubview:textView];
        textView.textColor =UIColorFromRGB(0x999999);


        yPos += textView.frame.size.height;
        //xPos += textView.frame.size.width;
        textView.frame = CGRectMake(xPos, yPos, textView.frame.size.width, textView.frame.size.height);

}

One Response to “How to correct set position of UIView from other UiView”

  1. Here is how can test a textView width compared to cell container width. You may also want to change the UITextView naming convention to tagView instead of textView for easier readability:

    for (int count = 0; count < Tags.count; count++) {
    
        UITextView *textView = [[UITextView alloc]init];
        textView.text = Tags[count][@"name"];
        [cell.viewForBaselineLayout addSubview:textView];
        textView.textColor =UIColorFromRGB(0x999999);    
    
    if(textView.frame.size.width > cell.frame.size.width){
    
      //Position textView on next line...
      yPos += textView.frame.size.height*2;
    
        } else {
    
      //Position textView on same line...
      yPos += textView.frame.size.height*1;
    
    }
      textView.position = CGRectMake(xPos,yPos, textView.frame.size.width, textView.frame.size.height);
    

Leave a Reply