动态添加标签
我需要一些指导。我正在解析 XML 并从中获取成员名称。名称应显示在标签上。现在我对任意数量的成员名称感到困惑。如何根据会员姓名的数量放置标签。
示例:如果我得到 5 个名称,则应显示 5 个带有名称的标签。
有什么好的编程指导吗?
我有一个滚动视图,并且已经添加了一个 UIVew 作为子视图来显示一些静态标签。现在名称的标签应该显示在 UIView 上。
谢谢..
I need some guidance. I am parsing an XML and get the members name from it. The name should be displayed on label . Now I am confused due to arbitrary number of members name. How can I put labels according to number of member's name.
Example : if I get 5 names then 5 label should be displayed with name .
any good programming guidance ?
I have a scrollview and already added a UIVew as a subview to display some static label. now name's label should be displayed on UIView.
Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设 memberNameArray 是包含要显示的名称的 NSString 数组。您还需要根据滚动视图中标签的位置更改框架的 x、y、宽度、高度和 y += 30 值。
Assuming memberNameArray is the array of NSString which contains the names to display. And also you need to change the
x, y, width, height
values of the frame andy += 30
based on the position of the labels in scroll view.下面的代码可用作根据数组值获取标签数量的参考。
为每个 UILabel 设置正确的框架...
The below code could be used as reference for getting number of label depending on Array values..
Set the correct frame for each UILabel...
UILabel *lblname=[[UILabel alloc]initWithFrame:CGRectMake(x,y,height,width)];
[lblname setText:@"任意文本"];
[lblname setBackgroundColor:[UIColor clearColor]];
[lblname setFont:[UIFont systemFontOfSize:15]];
[lblname setNumberOfLines:2];
[lblname setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:lblname];
UILabel *lblname=[[UILabel alloc]initWithFrame:CGRectMake(x,y,height,width)];
[lblname setText:@"any text"];
[lblname setBackgroundColor:[UIColor clearColor]];
[lblname setFont:[UIFont systemFontOfSize:15]];
[lblname setNumberOfLines:2];
[lblname setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:lblname];
for(int i = 0 ; i < [myArrayOfValue 计数]; i++)
{
UILabel* myLabel = [[UILabel alloc]initWithFrame:CGRectMake(myX,(myY + i*myHeight),myWidth,myHeight);
myLabel.text = [myArrayOfValue objectAtIndex:i];
for(int i = 0 ; i < [myArrayOfValue count]; i++)
{
UILabel* myLabel = [[UILabel alloc]initWithFrame:CGRectMake(myX,(myY + i*myHeight),myWidth,myHeight);
myLabel.text = [myArrayOfValue objectAtIndex:i];