Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(922)

Side by Side Diff: ios/chrome/browser/ui/util/CRUILabel+AttributeUtils.mm

Issue 2588733002: Upstream Chrome on iOS source code [9/11]. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ios/chrome/browser/ui/util/CRUILabel+AttributeUtils.h"
6
7 #import <objc/runtime.h>
8
9 #import "base/ios/weak_nsobject.h"
10 #include "base/mac/scoped_nsobject.h"
11 #import "ios/chrome/browser/ui/util/label_observer.h"
12
13 namespace {
14 // They key under which to associate the line height with the label.
15 const void* const kLineHeightKey = &kLineHeightKey;
16 // Creates an NSNumber from |line_height| and associates it with |label|.
17 void SetAssociatedLineHeight(CGFloat line_height, UILabel* label) {
18 objc_setAssociatedObject(label, kLineHeightKey, @(line_height),
19 OBJC_ASSOCIATION_RETAIN_NONATOMIC);
20 }
21 // Returns the line height associated with |label|.
22 CGFloat GetAssociatedLineHeight(UILabel* label) {
23 return [objc_getAssociatedObject(label, kLineHeightKey) floatValue];
24 }
25 }
26
27 @implementation UILabel (CRUILabelAttributeUtils)
28
29 // If there's no text in the label, the line height is whatever is stored in the
30 // associated value.
31 - (CGFloat)cr_lineHeight {
32 if (!self.text.length || !self.attributedText.string.length)
33 return GetAssociatedLineHeight(self);
34 NSParagraphStyle* style =
35 [self.attributedText attribute:NSParagraphStyleAttributeName
36 atIndex:0
37 effectiveRange:nullptr];
38 return style.maximumLineHeight;
39 }
40
41 - (void)cr_setLineHeight:(CGFloat)lineHeight {
42 DCHECK_GT(lineHeight, 0.0);
43 // If this is the first time the line height is being set, register the
44 // LabelObserverAction.
45 if (!GetAssociatedLineHeight(self)) {
46 LabelObserver* observer = [LabelObserver observerForLabel:self];
47 [observer addTextChangedAction:^(UILabel* label) {
48 label.cr_lineHeight = GetAssociatedLineHeight(label);
49 }];
50 }
51
52 // Store the new line height as an associated object.
53 SetAssociatedLineHeight(lineHeight, self);
54
55 // If there's no text yet, there's nothing that can be done. The
56 // LabelObserverAction will call this selector again upon changes to the text.
57 if (!self.text.length || !self.attributedText.string.length)
58 return;
59
60 base::scoped_nsobject<NSMutableAttributedString> newString(
61 [self.attributedText mutableCopy]);
62 DCHECK([newString length]);
63 NSParagraphStyle* style = [newString attribute:NSParagraphStyleAttributeName
64 atIndex:0
65 effectiveRange:nullptr];
66 if (!style)
67 style = [NSParagraphStyle defaultParagraphStyle];
68 base::scoped_nsobject<NSMutableParagraphStyle> newStyle([style mutableCopy]);
69 [newStyle setMinimumLineHeight:lineHeight];
70 [newStyle setMaximumLineHeight:lineHeight];
71 [newString addAttribute:NSParagraphStyleAttributeName
72 value:newStyle
73 range:NSMakeRange(0, [newString length])];
74 self.attributedText = newString;
75 }
76
77 - (void)cr_adjustLineHeightForMaximimumLines:(NSUInteger)maximumLines {
78 CGSize labelSize = self.bounds.size;
79 CGFloat lineHeight = self.cr_lineHeight;
80 CGFloat numberOfLines = floorf(labelSize.height / lineHeight);
81 CGSize maxSize = CGSizeMake(labelSize.width, CGFLOAT_MAX);
82 CGSize textSize = [self sizeThatFits:maxSize];
83
84 // |textSize.height| should be a multiple of |lineHeight|. If this is not the
85 // case, then it is safer to fit one more line to ensure that the text of the
86 // label is not cropped.
87 CGFloat requiredNumberOfLines = ceilf(textSize.height / lineHeight);
88 if (requiredNumberOfLines > numberOfLines) {
89 requiredNumberOfLines = MIN(requiredNumberOfLines, maximumLines);
90 self.cr_lineHeight = floorf(labelSize.height / requiredNumberOfLines);
91 }
92 }
93
94 @end
OLDNEW
« no previous file with comments | « ios/chrome/browser/ui/util/CRUILabel+AttributeUtils.h ('k') | ios/chrome/browser/ui/util/CRUILabel+AttributeUtils_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698