// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/shared/ui/table_view/cells/table_view_attributed_string_header_footer_item.h"
#import "base/check_op.h"
#import "base/containers/contains.h"
#import "ios/chrome/browser/net/model/crurl.h"
#import "ios/chrome/browser/shared/public/features/features.h"
#import "ios/chrome/browser/shared/ui/util/uikit_ui_util.h"
#import "ios/chrome/common/string_util.h"
#import "ios/chrome/common/ui/colors/semantic_color_names.h"
#import "ios/chrome/common/ui/table_view/table_view_cells_constants.h"
#import "ios/chrome/common/ui/util/text_view_util.h"
#import "net/base/apple/url_conversions.h"
@implementation TableViewAttributedStringHeaderFooterItem
- (instancetype)initWithType:(NSInteger)type {
self = [super initWithType:type];
if (self) {
self.cellClass = [TableViewAttributedStringHeaderFooterView class];
}
return self;
}
#pragma mark CollectionViewItem
- (void)configureHeaderFooterView:
(TableViewAttributedStringHeaderFooterView*)headerFooter
withStyler:(ChromeTableViewStyler*)styler {
[super configureHeaderFooterView:headerFooter withStyler:styler];
headerFooter.attributedString = self.attributedString;
}
@end
@interface TableViewAttributedStringHeaderFooterView () <UITextViewDelegate>
// UITextView corresponding to `text` from the item.
@property(nonatomic, readonly, strong) UITextView* textView;
@end
@implementation TableViewAttributedStringHeaderFooterView
- (instancetype)initWithReuseIdentifier:(NSString*)reuseIdentifier {
self = [super initWithReuseIdentifier:reuseIdentifier];
if (self) {
_textView = CreateUITextViewWithTextKit1();
_textView.scrollEnabled = NO;
_textView.editable = NO;
_textView.delegate = self;
_textView.backgroundColor = UIColor.clearColor;
_textView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
_textView.adjustsFontForContentSizeCategory = YES;
_textView.translatesAutoresizingMaskIntoConstraints = NO;
_textView.linkTextAttributes =
@{NSForegroundColorAttributeName : [UIColor colorNamed:kBlueColor]};
[self.contentView addSubview:_textView];
[NSLayoutConstraint activateConstraints:@[
[_textView.topAnchor constraintEqualToAnchor:self.contentView.topAnchor
constant:kTableViewVerticalSpacing],
[_textView.bottomAnchor
constraintEqualToAnchor:self.contentView.bottomAnchor
constant:-kTableViewVerticalSpacing],
[_textView.trailingAnchor
constraintEqualToAnchor:self.contentView.trailingAnchor
constant:-HorizontalPadding()],
[_textView.leadingAnchor
constraintEqualToAnchor:self.contentView.leadingAnchor
constant:HorizontalPadding()],
]];
}
return self;
}
- (void)prepareForReuse {
[super prepareForReuse];
self.textView.text = nil;
}
#pragma mark - Properties
- (void)setAttributedString:(NSAttributedString*)attributedString {
self.textView.attributedText = attributedString;
}
- (void)textViewDidChangeSelection:(UITextView*)textView {
// Always force the `selectedTextRange` to `nil` to prevent users from
// selecting text. Setting the `selectable` property to `NO` doesn't help
// since it makes links inside the text view untappable.
textView.selectedTextRange = nil;
}
@end