简体   繁体   English

iPhone Objective-C以编程方式创建标签

[英]iphone objective-c programmatically creating labels

I am creating the label programmatically with dynamic text, and next to label one more textview is present which is dynamic content, and next to text view there is an imageview. 我正在使用动态文本以编程方式创建标签,在标签旁边还有一个textview,它是动态内容,在文本视图旁边有一个imageview。 These three are not aligning correctly, 这三个没有正确对齐,

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

Are you creating the UITextView and UIImageView also programmatically? 您是否也在以编程方式创建UITextViewUIImageView If that is the case you just need to set their frame's 'x' or 'y' (depending on horizontal or vertical alignment) to the same value, like this: 如果是这种情况,您只需要将其框架的“ 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)];

I've had success with calling sizeToFit on UILabel objects then positioning the labels. 我已经成功通过在UILabel对象上调用sizeToFit然后定位标签。 First setup the label with the appropriate content and other settings, then call sizeToFit, then move them to the appropriate places. 首先使用适当的内容和其他设置设置标签,然后调用sizeToFit,然后将其移动到适当的位置。 For example I use this code to size and align two labels: 例如,我使用以下代码来调整和对齐两个标签:

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

Depending upon the content, I'll frequently have a space as the last character in the first label's text to make spacing work right. 根据内容的不同,我经常在第一个标签的文本中使用空格作为最后一个字符,以使间距正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM