@@ -53,13 +53,15 @@ - (instancetype) initWithFrame:(CGRect)frame {
5353- (void ) layoutSubviews {
5454 [super layoutSubviews ];
5555
56+ CGFloat width = self.bounds .size .width ;
57+
5658 // Top label
57- CGSize topSize = [self .topLabel sizeThatFits: CGSizeMake ( self .contentView.bounds.size.width - 20 . 0f , CGFLOAT_MAX) ];
58- self.topLabel .frame = CGRectMake (10 .0f , 9 .0f , topSize. width , topSize .height );
59+ CGRect topFrame = [self .class _topLabelFrameForString: self .topLabel.text withWidth: width ];
60+ self.topLabel .frame = CGRectMake (10 .0f , 9 .0f , topFrame. size . width , topFrame. size .height );
5961
6062 // Bottom label
61- CGSize bottomSize = [self .bottomLabel sizeThatFits: CGSizeMake ( self .contentView.bounds.size.width - 20 . 0f , CGFLOAT_MAX) ];
62- self.bottomLabel .frame = CGRectMake (10 .0f , CGRectGetMaxY (self.topLabel .frame ), bottomSize. width , bottomSize .height );
63+ CGRect bottomFrame = [self .class _bottomLabelFrameForString: self .bottomLabel.text withWidth: width ];
64+ self.bottomLabel .frame = CGRectMake (10 .0f , CGRectGetMaxY (self.topLabel .frame ), bottomFrame. size . width , bottomFrame. size .height );
6365}
6466
6567- (void )drawRect : (CGRect)rect {
@@ -80,15 +82,40 @@ + (NSString *) identifier {
8082
8183#pragma mark - Public methods
8284
83- - (CGFloat) maxSizeForInstruction : (AInstruction *)instruction withWidth : (CGFloat)width {
84- self.topLabel .text = instruction.mnemonic ;
85- self.bottomLabel .text = instruction.shortDesc ;
86-
85+ + (CGFloat) heightForInstruction : (AInstruction *)instruction withWidth : (CGFloat)width {
8786 CGFloat padding = 18 .0f ;
88- CGSize topSize = [self .topLabel sizeThatFits: CGSizeMake (width, CGFLOAT_MAX)];
89- CGSize bottomSize = [self .bottomLabel sizeThatFits: CGSizeMake (width, CGFLOAT_MAX)];
87+ CGRect topFrame = [self _topLabelFrameForString: instruction.mnemonic withWidth: width];
88+ CGRect bottomFrame = [self _bottomLabelFrameForString: instruction.shortDesc withWidth: width];
89+
90+ return topFrame.size .height + bottomFrame.size .height + padding;
91+ }
92+
93+ #pragma mark - Private class
94+
95+ + (CGRect) _topLabelFrameForString : (NSString *)string withWidth : (CGFloat)width {
96+ return [string boundingRectWithSize: CGSizeMake (width, CGFLOAT_MAX)
97+ options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
98+ attributes: self ._topLabelAttributes
99+ context: nil ];
100+ }
101+
102+ + (NSDictionary *) _topLabelAttributes {
103+ return @{
104+ NSFontAttributeName : [UIFont systemFontOfSize: 20 .0f weight: UIFontWeightBold]
105+ };
106+ }
107+
108+ + (CGRect) _bottomLabelFrameForString : (NSString *)string withWidth : (CGFloat)width {
109+ return [string boundingRectWithSize: CGSizeMake (width, CGFLOAT_MAX)
110+ options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
111+ attributes: self ._bottomLabelAttributes
112+ context: nil ];
113+ }
90114
91- return topSize.height + bottomSize.height + padding;
115+ + (NSDictionary *) _bottomLabelAttributes {
116+ return @{
117+ NSFontAttributeName : [UIFont systemFontOfSize: 14 .0f weight: UIFontWeightRegular]
118+ };
92119}
93120
94121#pragma mark - Setters
@@ -102,8 +129,8 @@ - (void) setHighlighted:(BOOL)highlighted {
102129- (void ) setInstruction : (AInstruction *)instruction {
103130 _instruction = instruction;
104131
105- self.topLabel .text = instruction.mnemonic ;
106- self.bottomLabel .text = instruction.shortDesc ;
132+ self.topLabel .attributedText = [[ NSAttributedString alloc ] initWithString: instruction.mnemonic attributes: self .class._topLabelAttributes] ;
133+ self.bottomLabel .attributedText = [[ NSAttributedString alloc ] initWithString: instruction.shortDesc attributes: self .class._bottomLabelAttributes] ;
107134
108135 [self setNeedsLayout ];
109136}
@@ -114,7 +141,6 @@ - (UILabel *) topLabel {
114141 if (!_topLabel) {
115142 _topLabel = [[UILabel alloc ] init ];
116143 _topLabel.backgroundColor = UIColor.clearColor ;
117- _topLabel.font = [UIFont systemFontOfSize: 20 .0f weight: UIFontWeightBold];
118144 _topLabel.textColor = UIColor.blackColor ;
119145 _topLabel.numberOfLines = 2 ;
120146 }
@@ -126,7 +152,6 @@ - (UILabel *) bottomLabel {
126152 if (!_bottomLabel) {
127153 _bottomLabel = [[UILabel alloc ] init ];
128154 _bottomLabel.backgroundColor = UIColor.clearColor ;
129- _bottomLabel.font = [UIFont systemFontOfSize: 14 .0f weight: UIFontWeightRegular];
130155 _bottomLabel.textColor = UIColor.blackColor ;
131156 _bottomLabel.numberOfLines = 0 ;
132157 _bottomLabel.lineBreakMode = NSLineBreakByWordWrapping;
0 commit comments