簡體   English   中英

將 uilabel 與輸入到 UITextField 的文本緊密相連

[英]Tether uilabel closely to the text being input to UITextField

我希望貨幣縮寫 uilabel 緊跟輸入到 UITextField 中的文本。 計算輸入的文本在哪里結束的好方法是什么

func rightViewRect(forBounds bounds: CGRect) -> CGRect

可以正確計算標簽矩形嗎?

除其他外,我最終得到了這個幫手:

func rightViewRect(bounds: CGRect,
                   label: UILabel,
                   field: UITextField
    ) -> CGRect
{
    let measure = UILabel()
    measure.font = field.font
    if field.text?.isEmpty ?? true {
        measure.text = field.placeholder
    } else {
        measure.text = field.text
    }
    let cs = measure.intrinsicContentSize
    let lcs = label.intrinsicContentSize
    guard lcs.width > 0 else {
        return .zero
    }
    let magicSpace = CGFloat(2)
    let unclipped = CGRect(x: cs.width + magicSpace, y: 0, width: lcs.width, height: bounds.height)
    let clipped = unclipped.intersection(bounds)
    return clipped
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM