OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ios/chrome/browser/ui/util/CRUILabel+AttributeUtils.h" | 5 #include "ios/chrome/browser/ui/util/CRUILabel+AttributeUtils.h" |
6 | 6 |
7 #import <objc/runtime.h> | 7 #import <objc/runtime.h> |
8 | 8 |
9 #import "base/ios/weak_nsobject.h" | 9 #include "base/logging.h" |
10 #include "base/mac/scoped_nsobject.h" | |
11 #import "ios/chrome/browser/ui/util/label_observer.h" | 10 #import "ios/chrome/browser/ui/util/label_observer.h" |
12 | 11 |
| 12 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 13 #error "This file requires ARC support." |
| 14 #endif |
| 15 |
13 namespace { | 16 namespace { |
14 // They key under which to associate the line height with the label. | 17 // They key under which to associate the line height with the label. |
15 const void* const kLineHeightKey = &kLineHeightKey; | 18 const void* const kLineHeightKey = &kLineHeightKey; |
16 // Creates an NSNumber from |line_height| and associates it with |label|. | 19 // Creates an NSNumber from |line_height| and associates it with |label|. |
17 void SetAssociatedLineHeight(CGFloat line_height, UILabel* label) { | 20 void SetAssociatedLineHeight(CGFloat line_height, UILabel* label) { |
18 objc_setAssociatedObject(label, kLineHeightKey, @(line_height), | 21 objc_setAssociatedObject(label, kLineHeightKey, @(line_height), |
19 OBJC_ASSOCIATION_RETAIN_NONATOMIC); | 22 OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
20 } | 23 } |
21 // Returns the line height associated with |label|. | 24 // Returns the line height associated with |label|. |
22 CGFloat GetAssociatedLineHeight(UILabel* label) { | 25 CGFloat GetAssociatedLineHeight(UILabel* label) { |
(...skipping 27 matching lines...) Expand all Loading... |
50 } | 53 } |
51 | 54 |
52 // Store the new line height as an associated object. | 55 // Store the new line height as an associated object. |
53 SetAssociatedLineHeight(lineHeight, self); | 56 SetAssociatedLineHeight(lineHeight, self); |
54 | 57 |
55 // If there's no text yet, there's nothing that can be done. The | 58 // 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. | 59 // LabelObserverAction will call this selector again upon changes to the text. |
57 if (!self.text.length || !self.attributedText.string.length) | 60 if (!self.text.length || !self.attributedText.string.length) |
58 return; | 61 return; |
59 | 62 |
60 base::scoped_nsobject<NSMutableAttributedString> newString( | 63 NSMutableAttributedString* newString = [self.attributedText mutableCopy]; |
61 [self.attributedText mutableCopy]); | |
62 DCHECK([newString length]); | 64 DCHECK([newString length]); |
63 NSParagraphStyle* style = [newString attribute:NSParagraphStyleAttributeName | 65 NSParagraphStyle* style = [newString attribute:NSParagraphStyleAttributeName |
64 atIndex:0 | 66 atIndex:0 |
65 effectiveRange:nullptr]; | 67 effectiveRange:nullptr]; |
66 if (!style) | 68 if (!style) |
67 style = [NSParagraphStyle defaultParagraphStyle]; | 69 style = [NSParagraphStyle defaultParagraphStyle]; |
68 base::scoped_nsobject<NSMutableParagraphStyle> newStyle([style mutableCopy]); | 70 NSMutableParagraphStyle* newStyle = [style mutableCopy]; |
69 [newStyle setMinimumLineHeight:lineHeight]; | 71 [newStyle setMinimumLineHeight:lineHeight]; |
70 [newStyle setMaximumLineHeight:lineHeight]; | 72 [newStyle setMaximumLineHeight:lineHeight]; |
71 [newString addAttribute:NSParagraphStyleAttributeName | 73 [newString addAttribute:NSParagraphStyleAttributeName |
72 value:newStyle | 74 value:newStyle |
73 range:NSMakeRange(0, [newString length])]; | 75 range:NSMakeRange(0, [newString length])]; |
74 self.attributedText = newString; | 76 self.attributedText = newString; |
75 } | 77 } |
76 | 78 |
77 @end | 79 @end |
OLD | NEW |