Ios iphone objective-c以编程方式创建标签

Ios iphone objective-c以编程方式创建标签,ios,iphone,alignment,Ios,Iphone,Alignment,我使用动态文本以编程方式创建标签,标签旁边还有一个文本视图,它是动态内容,文本视图旁边有一个图像视图。这三个没有正确对齐 _textView = [[[JSTwitterCoreTextView alloc] initWithFrame:CGRectMake(10,60,self.view.frame.size.width,130)] autorelease]; _textView.textColor=[UIColor whiteColor]; _textView.linkColor=[UICo

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

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

您是否也以编程方式创建
UITextView
UIImageView
?如果是这种情况,您只需将其帧的“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)];

我成功地在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)];
根据内容的不同,我经常会在第一个标签的文本中使用空格作为最后一个字符,以使间距正确