【问题标题】:Position where text starts inside a UILabel文本在 UILabel 内开始的位置
【发布时间】:2019-08-05 21:37:27
【问题描述】:

我有一个场景,我需要知道文本在 UILabel 中开始的确切位置。

我制作这张图片是为了简单地解释我需要知道的内容:

有没有可能得到它?!

【问题讨论】:

    标签: ios cocoa-touch uilabel


    【解决方案1】:

    为了得到你的标签的精确坐标,你需要将你的标签的坐标系转换为superview的坐标系。您可以使用以下方法执行此操作:

    From Documentation:

    convert(_:to:)

    从接收者的坐标转换一个矩形 系统到另一个视图。

    声明:

    func convert(_ rect: CGRect, to view: UIView?) -> CGRect

    用法:

    let pos = self.lbl.convert(self.lbl.bounds, to: self.view)
    print(pos.origin.x) //prints x position relative to superview
    print(pos.origin.y) //prints y position relative to superview
    

    【讨论】:

    • 我认为这不是 OP 想要的。他想找到标签内呈现的文本的坐标。
    • 正如@Losiowaty 所说的那样。
    猜你喜欢
    • 1970-01-01
    • 2014-06-28
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多