ios - 当我触摸 objective-c 中的标签时,如何根据标签的大小增加/减少矩形大小

标签 ios objective-c uiview uilabel

我在 subview 类中有一个矩形和两个标签,如 label1、label2。

  1. label1 固定在矩形内部,或者您可以说矩形内部有 label1。
  2. label2 在屏幕上距离 label1 有一段时间,它是可拖动的,因为我在我的 subview 中应用了 touches moved 方法并使 label2.userInteractionEnabled = YES
  3. 现在我想通过触摸连接 label2 和 label1,它工作正常,但矩形大小没有更新。
  4. 当我将 label2 与 label1 连接时,矩形大小也应更新为 label1+label2 大小以适合其中的两个标签。我如何获得这些东西... 这是我的代码。

    _targetRect = [[UIView alloc]init];
    self.frame = CGRectMake(10,70,70,30
    [self addsubView:_targetRect];
    

    对于与targetRect相同的x,y位置的Label

     label1 = [[UILabel alloc]init];
    self.frame = CGRectMake(10,70, width, hight)];
    [self addsubView: label1];
    

    对于 label2 x, y 位置远离标签 1

     label2 = [[UILabel2 alloc]init];
    self.frame = CGRectMake(100,70, width, hight)];
    label2.userInteractionEnabled = YES;
    [self addsubView:label2];
    

最佳答案

在label2框架设计后重新设计了矩形框大小。喜欢,

_targetRect.frame = CGRectMake(10,70, width, label1.frame,size.height+label2.frame,size.height)];

关于ios - 当我触摸 objective-c 中的标签时,如何根据标签的大小增加/减少矩形大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43449269/

相关文章:

ios - uitableview 删除 iOS 中的按钮图像

ios - 如何在考虑特殊字符的情况下选择字段中的文本?

ios - 如何检查沙箱服务器状态? ("Cannot connect to itunes store")

ios - UILabel 和 NSLayoutConstraint 的垂直列表

ios - 如何初始化 uiView 框架

ios - 在 iOS 7 下,如何使我的 UITextView 高度取决于运行应用程序的设备类型(4 英寸屏幕 vs 3.5)

ios - 直接从 bundle 访问 couchbase lite 数据库?

ios - 排序两个 NSMutableArrays

objective-c - 为什么我不能在 super 上调用核心数据访问器?

ios - 自动布局: Create constraint between two views in separate windows