A practical approach for sizing text to content?

90 views
Skip to first unread message

Spencer Kohan

unread,
Oct 8, 2017, 3:48:09 AM10/8/17
to overconstrained
I'm working on a simple constraint-based layout system, and I'm trying to figure out how to do content-based size constraints for text views.

The issue is, the text height is dependent on width, since the number of lines will change depending on how wide the text view is.

One solution I've considered is to split up the solving into a horizontal and vertical pass. I believe this would solve the issue, but it also adds a bit of complexity to the layout system for a single type of view since I don't currently classify layout variables or constraints in terms of direction.  Are there other solutions to this problem which are commonly used?

Håvard Fossli

unread,
Oct 10, 2017, 10:02:54 AM10/10/17
to overconstrained
Hello Spencer,

I am doing the same thing. Each time the solver changes variables for height or width on a textview I asynchronously flag those calculations and dirty. Then I calculate what I call intrinsicHeight and intrinsicWidth for that textview. Some variables may depend on this and cause some textviews width and height to change causing a second pass on dirty flags. Etc etc.

Example

Create model and autocreate constraints for auto maintained variables
auto add: text.intrinsicHeight == 0 
auto add: text.intrinsicWidth == 0

Set text 
text.label == "foo bar"
text.label changed => mark text.intrinsicHeight dirty
text.label changed => mark text.intrinsicWidth dirty

Parse constraints
add: text.height == text.intrinsicHeight
add: text.width == 200
text.width changed => mark text.intrinsicHeight dirty
add: text.top == 0
add: text.left == 0

Check for dirty flags
auto remove: text.intrinsicHeight == 0 
add: text.intrinsicHeight == 48 (calculated)
text.height changed => mark text.intrinsicWidth dirty

Check for dirty flags
auto remove: text.intrinsicWidth == 0 
add: text.intrinsicWidth == 103 (calculated)

Check for dirty flags
none => end

Håvard
Reply all
Reply to author
Forward
0 new messages