Uilabel获得框架宽度或高度


`lblAddress.frame.width`
`lblAddress.frame.size.width`

我搜索了很多次,但我仍然没有指出... lblAddress.frame.widthlblAddress.frame.size.width之间有什么区别。它给了我相同的输出..我不知道当?

请建议一些例子,以便我可以弄清楚整个事情。

它们几乎相同。一个是cgrect的宽度,另一个是cgsize的宽度。

唯一的区别是frame.width始终返回正值,因为frame.size.width在某些情况下可能会返回负值。

如果我只关心标准化的几何形状,我将直接使用速记frame.width

帧和大小具有宽度属性。您可能会感到困惑,因为大小也是框架的属性。就像我在问你我的鞋子颜色是什么颜色在我的情况下。

要了解更多信息,您可以查看如何构建结构。

struct CGRect {
  var origin: CGPoint
  var size: CGSize
}

为了添加一些额外的功能,Apple还提供了Cgrect的扩展,例如以下

extension CGRect {
    @available(iOS 2.0, *)
    public var height: CGFloat { get }
    @available(iOS 2.0, *)
    public var width: CGFloat { get }
}

现在回答您的问题。lbladdress.frame将返回cgrect

lblAddress.frame.width //This will use the extended functionality 
lblAddress.frame.size.width //this will use the actual struct
internally the width extension on CGRect will always use the same 
CGSize property from struct to give the width back and hence the same 
size property is accessed in both case, but the difference is the first 
one is standardized and hence only gives back positive values whereas 
the second will give the negative values as well.

您想找到原始点使用此代码这里 viewsend 是视图的出口

self.ViewSend.frame.origin.y

您想查找高峰,所以使用此代码

self.ViewSend.frame.size.height

您想找到宽度,所以请使用此代码

self.ViewSend.frame.size.width

它们几乎相同;但是,fram.width是唯一的值,但是frame.size.width可以用于设置或获得值

最新更新