简体   繁体   English

UITableView中的UILabel设置大小

[英]UILabel in UITableView setting size

I would like to add a label subview to a UITableViewCell. 我想将标签子视图添加到UITableViewCell。 I can easily set the frame origin and size myself to specific values but how can I use the current/default cell label height and position? 我可以轻松地将帧原点和尺寸设置为特定值,但是如何使用当前/默认单元格标签的高度和位置?

  UILabel *labPerHour;

  cell.textLabel.text       = @"Rate";

  labPerHour = [[UILabel alloc] init];

  //labPerHour.frame = CGRectMake( 280, cell.textLabel.frame.origin.y, 80, cell.textLabel.frame.size.height );
  labPerHour.frame = CGRectMake( 260, 7, 30, 30 );
  labPerHour.text  = @"$/hr";


  [cell addSubview:labPerHour];

I would like the commented-out line to work.. or something similar. 我希望注释掉的行可以工作..或类似的东西。

Can somebody please help? 有人可以帮忙吗? This code is of course part of my cellForRowAtIndexPath method. 这段代码当然是我的cellForRowAtIndexPath方法的一部分。

I am not sure. 我不确定。 May be it will work. 可能会起作用。 I haven't tried this before!! 我以前没有尝试过!

CGRect theFrame = cell.textLabel.frame;
theFrame.origin.x = 280;
theFrame.size.width = 80;
labPerHour.frame = theFrame;

If you trying to get the frame of default label of uitable view, then you may try this 如果您尝试获取可查看视图的默认标签框架,则可以尝试此操作

CGRect myframe;
myframe= cell.textlabel.frame;

now use this as per need 现在根据需要使用它

You need to provide coordinates relative to the superview's coordinate system. 您需要提供相对于超级视图的坐标系的坐标。 These coordinates need to make sense, so your label is still positioned inside your cell's frame, not outside it. 这些坐标必须有意义,因此标签仍位于单元格框架内,而不是单元格外部。

Because every view and window defines its own local coordinate system, you need to be aware of which coordinate system is in effect at any given time. 因为每个视图和窗口都定义了自己的局部坐标系,所以您需要知道在任何给定时间有效的坐标系。 Every time you draw into a view or change its geometry, you do so relative to some coordinate system. 每次绘制视图或更改其几何形状时,都是相对于某个坐标系进行的。 In the case of drawing, you specify coordinates relative to the view's own coordinate system. 对于图形,可以指定相对于视图自身坐标系的坐标。 In the case of geometry changes, you specify coordinates relative to the superview's coordinate system. 如果几何发生变化,则可以指定相对于超级视图的坐标系的坐标。 The UIWindow and UIView classes both include methods to help you convert from one coordinate system to another. UIWindow和UIView类都包含帮助您从一个坐标系转换为另一个坐标系的方法。

Section "View Geometry and Coordinate Systems" of View Programming Guide for iOS . 适用于iOSView编程指南的 “视图几何和坐标系”部分。

您首先需要设置单元格标签框架。

cell.textLabel.frame = CGRectMake(0, 0, 40, 10);

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

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