2017-02-19 14 views
2

所以我有兩個UIViews,一個UILabelUITextField,第一個是字段的標題,第二個是信息將鍵入..這是我如何「M設置其自動版式限制條件:UILabel的寬度超過了文本使用編程自動佈局

對於輸入字段:

//Constraints: 
    inputField.translatesAutoresizingMaskIntoConstraints = false; 
    //Setting trailing = superview(which is called self)'s trailing +2.0 
    NSLayoutConstraint(item: inputField, 
    attribute: .trailing, relatedBy: .equal, 
    toItem: self, attribute: .trailingMargin, 
    multiplier: 1.0, constant: 2.0).isActive = true; 

    //Set Top = superview(which is called self) top -2.0 
    NSLayoutConstraint(item: inputField, 
    attribute: .top, relatedBy: .equal, 
    toItem: self, attribute: .topMargin, 
    multiplier: 1.0, constant: -2.0).isActive = true; 

    //Setting the height = 30 
    NSLayoutConstraint(item: inputField, attribute: .height, 
    relatedBy: .equal, 
    toItem: nil, attribute: .notAnAttribute, 
    multiplier: 1.0, constant: 30).isActive = true; 

然後用於的UILabel(稱爲emailLabel22):

//Constraints: 
    //  Left 
    NSLayoutConstraint(item: emailLabel22, 
    attribute: .leading, relatedBy: .equal, 
    toItem: self, attribute: .leadingMargin, 
    multiplier: 1.0, constant: 2.0).isActive = true; 

    //  Buffer Right 
    NSLayoutConstraint(item: emailLabel22, 
    attribute: .trailing, relatedBy: .equal, 
    toItem: inputField, attribute: .leading, 
    multiplier: 1.0, constant: -8.0).isActive = true; 

    //  Align Tops 
    NSLayoutConstraint(item: emailLabel22, 
    attribute: .top, relatedBy: .equal, 
    toItem: inputField, attribute: .top, 
    multiplier: 1.0, constant: 0).isActive = true; 

    //  Height 
    NSLayoutConstraint(item: emailLabel22, 
    attribute: .height, relatedBy: .equal, 
    toItem: nil, attribute: .notAnAttribute, 
    multiplier: 1.0, constant: 30).isActive = true; 

    //  Buffer Right -- **For Input Field** 
    NSLayoutConstraint(item: inputField, 
    attribute: .leading, relatedBy: .equal, 
    toItem: emailLabel22, attribute: .trailing, 
    multiplier: 1.0, constant: 8.0).isActive = true; 

    //  Locking in Width??? (Should I use this?) 
    NSLayoutConstraint(item: emailLabel22, 
    attribute: .width, relatedBy: .equal, 
    toItem: nil, attribute: .notAnAttribute, 
    multiplier: 1.0, constant: emailLabel22.frame.size.width); 

以上代碼的要點是:inputField與超級視圖右對齊(self)。電子郵件是左對齊的。然後他們之間有一個美學差距。

這就是問題所在:

如果我設置emailLabel22第一,那麼它的寬度變得龐大和輸入字段被壓扁在屏幕的右側,與他們之間有很多空的空間。 Like so.

如果我先設置輸入字段,如上面的代碼所示,然後將郵件壓扁到其鎖定寬度,在本例中爲73.這意味着任何不符合73pt寬度的文本被省略號截斷。 Like So

如果我刪除電子郵件寬度上的鎖定,則輸入字段的大小會擴展,如果我輸入的字符不適合文本字段。 Like So

這不會是一個真正的問題,但我試圖建立一個強大的標籤/文本字段組合,我可以設置任何文本並完美匹配,因此我不想「鎖定電子郵件標籤的寬度」。我希望它儘可能多地擴展到正確的位置,但在文本大小範圍內。我也不希望inputField擠壓電子郵件標籤。我也不想壓縮電子郵件的字體大小,輸入壓縮很好。

我一直在試圖解決這個問題3個小時。我不知道如何。

謝謝。

+1

這深夜我在哪裏,所以我沒有時間去幫助,但蘋果確實有一些很好的文檔關於「內在大小」,看起來可能會有所幫助:https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ViewswithIntrinsicContentSize.html – dfd

回答

2

嘗試集擁抱和commpresion性優先於你的UILabel,同時也是UITextField

enter image description here

+0

我從來不知道如何使用這些!現在我明白了!!非常感謝( – QuantumHoneybees

+0

我也永遠不會記住,所以我總是在電腦上有這個圖像 –

+0

你介意分享你從哪裏得到這個圖像嗎?我喜歡它! – QuantumHoneybees

相關問題