2017-03-22 9 views
0

행에 정렬해야하는 UITextView와 UILabel이 있습니다. textView는 여러 줄로 구성되며 너비는 레이블에 따라 정렬되어야합니다. 레이블은 단일 행이지만 너비가 가변적입니다. 그러나 레이블의 너비는 textView의 너비보다 높은 우선 순위를 가져야합니다. 이제 문제는 텍스트 뷰가 맞게 밖으로 누워있는 것처럼 보이는 것을하고 레이블이 남은 공간을 처리 할 수있다 : 여기 there should be "99" displayed right of the chat bubbleUITextView와 UILabel을 신속하게 정렬합니다.

또 다른 예이지만,이 시간의 텍스트 레이블이 올바르게 표시되도록 텍스트 뷰가 들어갈 정도로 짧다 :

enter image description here

가 어떻게 내가 먼저 레이아웃을 할 레이블을 강제 할 수 있으며, 그 후, 텍스트 뷰가 왼쪽 공간 먹으 렴을 사용할 수 있습니다 (이 멀티 라인이기 때문에 문제가되지 않습니다.) 항목의

초기화 : 파단을 추가 한 후

let captionTextView: UITextView = { 
    let textView = UITextView() 
    textView.translatesAutoresizingMaskIntoConstraints = false 
    textView.text = "" 
    textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0) 
    textView.textColor = .white 
    textView.backgroundColor = .green 
    textView.font = .systemFont(ofSize: 16) 
    textView.isScrollEnabled = false 
    textView.sizeToFit() 
    return textView 
}() 

/////////////////////////////////// 
//Comments 
var commentsIcon: UIButton = { 
    let cb = UIButton() 
    cb.setImage(#imageLiteral(resourceName: "chatBubble"), for: .normal) 
    return cb 
}() 
var commentsTextLabel: UILabel = { 
    let label = UILabel() 
    label.translatesAutoresizingMaskIntoConstraints = false 
    label.text = "99" 
    label.textColor = .white 
    label.backgroundColor = .green 
    label.font = .systemFont(ofSize: 14) 
    label.sizeToFit() 
    label.numberOfLines = 1 
    return label 
}() 

레이아웃 세부 정보 :

//horizontal constraints 

self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]-5-[v1]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": captionTextView, "v1": commentsIcon])) 
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[v0(15)]-4-[v1]-5-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": commentsIcon, "v1": commentsTextLabel])) 

답변

1

당신은 모두 당신의 라벨과 텍스트 뷰에 contentCompressionResistancePrioritycontentHuggingPriority을 조정해야합니다.

레이블의 텍스트 뷰보다 contentCompressionResistancePriority이 더 크어야합니다.

+0

난 그냥 추가 : 'label.setContentCompressionResistancePriority (UILayoutPriorityDefaultHigh에 대한 : .horizontal)' 'label.setContentHuggingPriority (UILayoutPriorityDefaultHigh에 대한 : .horizontal)' 및 'textView.setContentCompressionResistancePriority (UILayoutPriorityDefaultLow에 대한 : .horizontal) ' textView.setContentHuggingPriority (UILayoutPriorityDefaultLow, for : .horizontal)' 그리고 완벽하게 작동합니다! 감사 :) –