LinkTableViewCell.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "LinkTableViewCell.h"
  17. static const NSUInteger kHInset = 10;
  18. static const NSUInteger kVInset = 4;
  19. @implementation LinkTableViewCell {
  20. UILabel *_titleLabel;
  21. UITextView *_linkTextView;
  22. }
  23. - (instancetype)init {
  24. self = [super initWithStyle:UITableViewCellStyleDefault
  25. reuseIdentifier:NSStringFromClass(self.class)];
  26. if (self) {
  27. _titleLabel = [[UILabel alloc] init];
  28. _titleLabel.font = [UIFont systemFontOfSize:15];
  29. _linkTextView = [[UITextView alloc] init];
  30. _linkTextView.font = [UIFont boldSystemFontOfSize:15];
  31. _linkTextView.editable = NO;
  32. _linkTextView.scrollEnabled = NO;
  33. _linkTextView.dataDetectorTypes = UIDataDetectorTypeLink;
  34. [self.contentView addSubview:_titleLabel];
  35. [self.contentView addSubview:_linkTextView];
  36. }
  37. return self;
  38. }
  39. - (void)layoutSubviews {
  40. _titleLabel.frame = CGRectMake(kHInset, kVInset, self.contentView.frame.size.width - 2 * kHInset,
  41. (self.contentView.frame.size.height / 2) - 2 * kVInset);
  42. _linkTextView.frame = CGRectMake(kHInset, (self.contentView.frame.size.height / 2) + kVInset,
  43. self.contentView.frame.size.width - 2 * kHInset,
  44. (self.contentView.frame.size.height / 2) - 2 * kVInset);
  45. }
  46. - (void)setTitle:(NSString *)title link:(NSString *)link {
  47. self.accessibilityIdentifier =
  48. [NSString stringWithFormat:@"%@-%@", NSStringFromClass(self.class), title];
  49. _linkTextView.accessibilityIdentifier =
  50. [NSString stringWithFormat:@"%@-LinkTextView-%@", NSStringFromClass(self.class), title];
  51. _titleLabel.text = title;
  52. if (link) {
  53. NSURL *URL = [NSURL URLWithString:link];
  54. NSAttributedString *attributedLink =
  55. [[NSAttributedString alloc] initWithString:link attributes:@{NSLinkAttributeName : URL}];
  56. _linkTextView.attributedText = attributedLink;
  57. }
  58. _linkTextView.accessibilityValue = link;
  59. }
  60. @end