Skip to content

Commit e59b8ac

Browse files
authored
[iOS] Updated RichTextBlock wrap behavior (microsoft#6399)
* [iOS] Updated RichTextBlock wrap behavior * [iOS] Refactored ActionSet constraints and removed auxiliary width constraints that caused text to clipped
1 parent 2e74a07 commit e59b8ac

File tree

4 files changed

+100
-93
lines changed

4 files changed

+100
-93
lines changed

source/ios/AdaptiveCards/AdaptiveCards/AdaptiveCards/ACRActionSetRenderer.mm

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,13 @@ - (UIView *)renderButtons:(ACRView *)rootView
183183
containingView.contentview = childview;
184184
containingView.contentWidth = contentWidth;
185185

186-
[containingView.heightAnchor constraintEqualToAnchor:childview.heightAnchor].active = YES;
187-
if (ActionsOrientation::Vertical == adaptiveActionConfig.actionsOrientation) {
188-
[containingView.widthAnchor constraintEqualToAnchor:childview.widthAnchor].active = YES;
189-
}
190-
containingView.translatesAutoresizingMaskIntoConstraints = NO;
191-
192186
containingView.stretch = adaptiveActionConfig.actionAlignment == ActionAlignment::Stretch;
193187

188+
[containingView preconfigreAutolayout];
189+
190+
// let layout subview to configure correct constraints when the frame size is available
191+
[NSLayoutConstraint activateConstraints:containingView.nonStretchConstraints];
192+
194193
// this step ensures that action set view is added before subviews added by show cards
195194
[superview insertArrangedSubview:containingView atIndex:stackIndex];
196195

@@ -214,30 +213,30 @@ - (void)renderButtonForElem:(ACOBaseActionElement *)acoElem
214213

215214
UIButton *button = nil;
216215

217-
@try {
218-
if ([acoElem meetsRequirements:featureReg] == NO) {
219-
@throw [ACOFallbackException fallbackException];
220-
}
221-
button = [actionRenderer renderButton:rootView
222-
inputs:inputs
223-
superview:superview
224-
baseActionElement:acoElem
225-
hostConfig:config];
226-
227-
configRtl(button, rootView.context);
228-
229-
[childview addArrangedSubview:button];
230-
} @catch (ACOFallbackException *exception) {
231-
handleActionFallbackException(exception, superview, rootView, inputs, acoElem, config,
232-
childview);
233-
NSUInteger count = [childview.arrangedSubviews count];
234-
if (count > numElem) {
235-
UIView *view = [childview.arrangedSubviews lastObject];
236-
if (view && [view isKindOfClass:[UIButton class]]) {
237-
button = (UIButton *)view;
238-
}
239-
}
240-
}
216+
@try {
217+
if ([acoElem meetsRequirements:featureReg] == NO) {
218+
@throw [ACOFallbackException fallbackException];
219+
}
220+
button = [actionRenderer renderButton:rootView
221+
inputs:inputs
222+
superview:superview
223+
baseActionElement:acoElem
224+
hostConfig:config];
225+
226+
configRtl(button, rootView.context);
227+
228+
[childview addArrangedSubview:button];
229+
} @catch (ACOFallbackException *exception) {
230+
handleActionFallbackException(exception, superview, rootView, inputs, acoElem, config,
231+
childview);
232+
NSUInteger count = [childview.arrangedSubviews count];
233+
if (count > numElem) {
234+
UIView *view = [childview.arrangedSubviews lastObject];
235+
if (view && [view isKindOfClass:[UIButton class]]) {
236+
button = (UIButton *)view;
237+
}
238+
}
239+
}
241240

242241
accumulatedWidth += [button intrinsicContentSize].width;
243242
accumulatedHeight += [button intrinsicContentSize].height;

source/ios/AdaptiveCards/AdaptiveCards/AdaptiveCards/ACRContentHoldingUIScrollView.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
@property UIStackView *contentview;
1515
@property CGFloat spacing;
1616
@property CGFloat contentWidth;
17-
@property NSLayoutConstraint *widthConstraintForStretch;
18-
@property NSLayoutConstraint *centerXConstraintForStretch;
19-
@property NSLayoutConstraint *centerYConstraintForStretch;
17+
18+
// collection of constraints that will be used when
19+
// buttons will only occupy just enough space
20+
@property NSArray<NSLayoutConstraint *> *nonStretchConstraints;
21+
// collection of constraints that will be used when buttons will
22+
// streteched to fill the available width
23+
@property NSArray<NSLayoutConstraint *> *stretchConstraints;
24+
25+
- (void)preconfigreAutolayout;
2026

2127
@end

source/ios/AdaptiveCards/AdaptiveCards/AdaptiveCards/ACRContentHoldingUIScrollView.mm

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,58 @@
99

1010
@implementation ACRContentHoldingUIScrollView
1111

12+
- (void)preconfigreAutolayout
13+
{
14+
if (!self.contentview) {
15+
return;
16+
}
17+
18+
self.translatesAutoresizingMaskIntoConstraints = NO;
19+
self.stretchConstraints = [[NSMutableArray alloc] init];
20+
self.nonStretchConstraints = [[NSMutableArray alloc] init];
21+
22+
[(NSMutableArray *)self.stretchConstraints addObject:[self.heightAnchor constraintEqualToAnchor:self.contentview.heightAnchor]];
23+
[(NSMutableArray *)self.nonStretchConstraints addObject:[self.heightAnchor constraintEqualToAnchor:self.contentview.heightAnchor]];
24+
25+
[(NSMutableArray *)self.stretchConstraints addObjectsFromArray:@[
26+
[self.contentview.widthAnchor constraintEqualToAnchor:self.widthAnchor
27+
multiplier:1.0],
28+
[self.contentview.centerXAnchor constraintEqualToAnchor:self.centerXAnchor],
29+
[self.contentview.centerYAnchor constraintEqualToAnchor:self.centerYAnchor]
30+
]];
31+
32+
[(NSMutableArray *)self.nonStretchConstraints addObjectsFromArray:@[
33+
[NSLayoutConstraint constraintWithItem:self
34+
attribute:NSLayoutAttributeTop
35+
relatedBy:NSLayoutRelationEqual
36+
toItem:self.contentview
37+
attribute:NSLayoutAttributeTop
38+
multiplier:1.0
39+
constant:0],
40+
[NSLayoutConstraint constraintWithItem:self
41+
attribute:NSLayoutAttributeBottom
42+
relatedBy:NSLayoutRelationEqual
43+
toItem:self.contentview
44+
attribute:NSLayoutAttributeBottom
45+
multiplier:1.0
46+
constant:0],
47+
[NSLayoutConstraint constraintWithItem:self
48+
attribute:NSLayoutAttributeLeading
49+
relatedBy:NSLayoutRelationEqual
50+
toItem:self.contentview
51+
attribute:NSLayoutAttributeLeading
52+
multiplier:1.0
53+
constant:0],
54+
[NSLayoutConstraint constraintWithItem:self
55+
attribute:NSLayoutAttributeTrailing
56+
relatedBy:NSLayoutRelationEqual
57+
toItem:self.contentview
58+
attribute:NSLayoutAttributeTrailing
59+
multiplier:1.0
60+
constant:0]
61+
]];
62+
}
63+
1264
- (CGSize)intrinsicContentSize
1365
{
1466
// whenever intrinsic content size is called, re-check if content size for subviews have changed
@@ -36,17 +88,12 @@ - (void)layoutSubviews
3688
self.contentWidth = [self intrinsicContentSize].width;
3789

3890
// if content size is smaller than the superview, and stretch is specified, stretch content view
39-
if (self.contentview && (self.stretch && self.frame.size.width > self.contentWidth && !self.widthConstraintForStretch.active)) {
91+
if (self.contentview && (self.stretch && self.frame.size.width > self.contentWidth &&
92+
self.stretchConstraints &&
93+
self.stretchConstraints.count > 0 &&
94+
!self.stretchConstraints[0].active)) {
4095
// add new constraints before layoutSubview before layout pass
41-
self.widthConstraintForStretch = [self.contentview.widthAnchor constraintEqualToAnchor:self.widthAnchor multiplier:1.0];
42-
self.widthConstraintForStretch.active = YES;
43-
44-
self.centerXConstraintForStretch = [self.contentview.centerXAnchor constraintEqualToAnchor:self.centerXAnchor];
45-
self.centerXConstraintForStretch.active = YES;
46-
47-
self.centerYConstraintForStretch = [self.contentview.centerYAnchor constraintEqualToAnchor:self.centerYAnchor];
48-
self.centerYConstraintForStretch.active = YES;
49-
96+
[NSLayoutConstraint activateConstraints:self.stretchConstraints];
5097
// layout pass
5198
[super layoutSubviews];
5299
// everything is done
@@ -58,53 +105,8 @@ - (void)layoutSubviews
58105
if ((self.contentview.axis == UILayoutConstraintAxisHorizontal) && self.frame.size.width < self.contentWidth && !_isContentSizeConstraintSet) {
59106
_isContentSizeConstraintSet = YES;
60107
// de-activate constraints
61-
if (self.widthConstraintForStretch) {
62-
self.widthConstraintForStretch.active = NO;
63-
}
64-
65-
if (self.centerXConstraintForStretch) {
66-
self.centerXConstraintForStretch.active = NO;
67-
}
68-
69-
if (self.centerYConstraintForStretch) {
70-
self.centerYConstraintForStretch.active = NO;
71-
}
72-
73-
// following constraints enables scrolling behaviors
74-
[NSLayoutConstraint constraintWithItem:self
75-
attribute:NSLayoutAttributeTop
76-
relatedBy:NSLayoutRelationEqual
77-
toItem:self.contentview
78-
attribute:NSLayoutAttributeTop
79-
multiplier:1.0
80-
constant:0]
81-
.active = YES;
82-
[NSLayoutConstraint constraintWithItem:self
83-
attribute:NSLayoutAttributeBottom
84-
relatedBy:NSLayoutRelationEqual
85-
toItem:self.contentview
86-
attribute:NSLayoutAttributeBottom
87-
multiplier:1.0
88-
constant:0]
89-
.active = YES;
90-
[NSLayoutConstraint constraintWithItem:self
91-
attribute:NSLayoutAttributeLeading
92-
relatedBy:NSLayoutRelationEqual
93-
toItem:self.contentview
94-
attribute:NSLayoutAttributeLeading
95-
multiplier:1.0
96-
constant:0]
97-
.active = YES;
98-
[NSLayoutConstraint constraintWithItem:self
99-
attribute:NSLayoutAttributeTrailing
100-
relatedBy:NSLayoutRelationEqual
101-
toItem:self.contentview
102-
attribute:NSLayoutAttributeTrailing
103-
multiplier:1.0
104-
constant:0]
105-
.active = YES;
106-
107-
108+
[NSLayoutConstraint deactivateConstraints:self.stretchConstraints];
109+
[NSLayoutConstraint activateConstraints:self.nonStretchConstraints];
108110
// now ready for layout pass
109111
[super layoutSubviews];
110112
}

source/ios/AdaptiveCards/AdaptiveCards/AdaptiveCards/ACRRichTextBlockRenderer.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,6 @@ - (UIView *)render:(UIView<ACRIContentHoldingView> *)viewGroup
187187

188188
lab.translatesAutoresizingMaskIntoConstraints = NO;
189189

190-
[viewGroup addArrangedSubview:lab];
191-
192190
HorizontalAlignment adaptiveAlignment = rTxtBlck->GetHorizontalAlignment().value_or(HorizontalAlignment::Left);
193191

194192
if (adaptiveAlignment == HorizontalAlignment::Left) {
@@ -221,7 +219,9 @@ - (UIView *)render:(UIView<ACRIContentHoldingView> *)viewGroup
221219
[lab setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
222220

223221
configRtl(lab, rootView.context);
224-
222+
223+
[viewGroup addArrangedSubview:lab];
224+
225225
return lab;
226226
}
227227

0 commit comments

Comments
 (0)