I want the text label adjust its position based on the green view. It should left alignment to the view if possible, but if the text content extent out of the dashboard, I'd like the text to right alignment to the view. Example:

The green view's position and changed randomly, I don't want to calculate the text frame every time and manually enable or disable different constraints, or set frame directly. I really want to explore some easy way to achieve that
Currently I uses:
C1 = [text.leadingAnchor constraintEqualToAnchor:view.leadingAnchor]
C2 = [text.trailingAnchor constraintLessThanOrEqualToAnchor:view.trailingAnchor]
C3 = [text.trailingAnchor constraintLessThanOrEqualToAnchor:dashboard.trailingAnchor]
priority: C3>C2>C1
But in that case, the text position for the middle green view is wrong.





Your question, effectively, whether you could have it right aligned with the green view if it would fit, and otherwise left align it. There is no simple set of constraints that will be able to do that automatically. You could theoretically have two constraints, one for left alignment and another for right alignment, and then programmatically activate one and deactivate the other based upon where the green view was within its superview. (Or, needless to say, you could go completely old-school, and adjust frames manually.)
But there is a simple alternative that keeps it entirely within the scope of the constraints system, without any manual adjustments, but it isn’t precisely what you asked for. But, it might be sufficient for your purposes: Specifically, you could right align it with the green view if there is space, and left align it with the green view’s superview if not.
The basic idea is to have required constraints for the label with respect to the container (your dashboard view), but a lower priority constraint with respect to the green view’s trailing edge.
You might also want to set the priorities of the label’s content hugging and content compression resistance. These priorities should be higher than the green view’s trailing anchor constraint. E.g.:
Yields:
Or, in Objective-C:
Or, needless to say, you can set all of these constraints and priorities in Interface Builder, not requiring any code at all. But hopefully this illustrates how to use priorities to achieve the desired effect.