【问题标题】:iphone objective-c programmatically creating labelsiphone objective-c 以编程方式创建标签
【发布时间】:2012-03-30 21:31:08
【问题描述】:

我正在使用动态文本以编程方式创建标签,并且在标签旁边还有一个文本视图,它是动态内容,在文本视图旁边有一个图像视图。这三个没有正确对齐,

_textView = [[[JSTwitterCoreTextView alloc] initWithFrame:CGRectMake(10,60,self.view.frame.size.width,130)] autorelease];
_textView.textColor=[UIColor whiteColor];
_textView.linkColor=[UIColor lightTextColor];
[self.view addSubview:_textView];
CGFloat Lsize=_textView.bounds.size.height+_textView.bounds.origin.y;
//CGFloat width = [UIScreen mainScreen].bounds.size.width - 50;
//CGFloat height = [self textHeight:text] + 10;
//CGRect frame = CGRectMake(10.0f, 10.0f, width, height);
startLine=[[UILabel alloc]initWithFrame:CGRectMake(0,Lsize, 320, 2)];
[self.view addSubview:startLine];

【问题讨论】:

    标签: ios iphone alignment


    【解决方案1】:

    您是否也在以编程方式创建UITextViewUIImageView?如果是这种情况,您只需将其框架的“x”或“y”(取决于水平或垂直对齐方式)设置为相同的值,如下所示:

    // Vertical alignment
    UILabel *label = [[UILabel alloc] initWithFrame:           CGRectMake(labelX, y, labelWidth, labelHeight)];
    UITextView *text = [[UITextView alloc] initWithFrame:      CGRectMake(textX, y, textWidth, textHeight)];
    UIImageView *image = [[UIImageView alloc] initWithFrame:   CGRectMake(imageX, y, imageWidth, imageHeight)];
    

    【讨论】:

      【解决方案2】:

      我已经成功地在 UILabel 对象上调用 sizeToFit 然后定位标签。首先使用适当的内容和其他设置设置标签,然后调用 sizeToFit,然后将它们移动到适当的位置。例如,我使用此代码来调整两个标签的大小并对齐:

      [countDescriptorLabel sizeToFit];
      [countOutputLabel sizeToFit];
          [countOutputLabel setFrame:CGRectMake(countDescriptorLabel.frame.origin.x + countDescriptorLabel.frame.size.width, countOutputLabel.frame.origin.y, countOutputLabel.frame.size.width, countOutputLabel.frame.size.height)];
      

      根据内容,我会经常在第一个标签的文本中使用空格作为最后一个字符,以使空格正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多