Created
January 12, 2015 04:51
-
-
Save codetalks-new/f49ac863816bfc35b91f to your computer and use it in GitHub Desktop.
Simple CommentView fix use textLabel.intrincContentSize() bug can auto newline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CommentView:UIControl{ | |
let nameView = UILabel() | |
let textLabel = UILabel() | |
let innerSpace:CGFloat = 4 | |
var insets:UIEdgeInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8){ | |
didSet{ | |
invalidateIntrinsicContentSize() | |
} | |
} | |
class var identifier:String{ | |
return "CommentView" | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setup() | |
} | |
func setup(){ | |
setTranslatesAutoresizingMaskIntoConstraints(false) | |
backgroundColor = UIColor.grayColor() | |
let contentView = self // for non UICollectionViewCell | |
contentView.addSubview(nameView) | |
contentView.addSubview(textLabel) | |
nameView.setTranslatesAutoresizingMaskIntoConstraints(false) | |
textLabel.setTranslatesAutoresizingMaskIntoConstraints(false) | |
nameView.backgroundColor = UIColor.blueColor() | |
textLabel.backgroundColor = UIColor.brownColor() | |
textLabel.font = UIFont.systemFontOfSize(13) | |
textLabel.numberOfLines = 2 | |
textLabel.lineBreakMode = .ByTruncatingTail | |
textLabel.textColor = UIColor.darkTextColor() | |
nameView.font = UIFont.systemFontOfSize(12) | |
nameView.textColor = blueColor | |
textLabel.setContentCompressionResistancePriority(20, forAxis: .Horizontal) | |
textLabel.setContentHuggingPriority(50, forAxis: .Vertical) | |
addTopConstraintForSubView(nameView, withConstant: insets.top) | |
addLeadingConstraintForSubView(nameView, withConstant: insets.left) | |
addTrailingConstraintForSubView(textLabel, withConstant: insets.right) // FIXME can satisfy | |
addTopConstraintForSubView(textLabel, withConstant: insets.top) | |
addBottomConstraintForSubView(textLabel, withConstant: insets.bottom) | |
addHorizontalSpaceConstraintForView(textLabel, toRightOfView: nameView, withConstant: innerSpace) | |
} | |
func bind(comment:Comment){ | |
nameView.text = comment.name | |
textLabel.text = ":" + comment.text | |
invalidateIntrinsicContentSize() | |
layoutIfNeeded() | |
} | |
override func intrinsicContentSize() -> CGSize { | |
let maxWidth:CGFloat = 200 | |
let nameSize = nameView.intrinsicContentSize() | |
let textSize = textLabel.sizeThatFits(CGSize(width: maxWidth - nameSize.width - innerSpace, height: 0 )) | |
// let textSize = textLabel.intrinsicContentSize() | |
let width = insets.left + nameSize.width + innerSpace + textSize.width + insets.right | |
let height = max(nameSize.height, textSize.height) + insets.top + insets.bottom | |
return CGSize(width: width, height: height) | |
} | |
required init(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment