@@ -12,9 +12,11 @@ public enum LabelNodeString {
1212
1313public class LabelNode : AnyNode {
1414
15+ private let numberOfLines : Int
1516 private let text : LabelNodeString
16- public init < T: UILabel > ( tag: Taggable , text: LabelNodeString , subnodes: [ AnyNode ] = [ ] , initializer: ( T ? ) -> T ) {
17+ public init < T: UILabel > ( tag: Taggable , text: LabelNodeString , numberOfLines : Int = 1 , subnodes: [ AnyNode ] = [ ] , initializer: ( T ? ) -> T ) {
1718 self . text = text
19+ self . numberOfLines = numberOfLines
1820 super. init ( tag: tag, subnodes: subnodes) { ( label: T ? ) -> T in
1921
2022 let l = initializer ( label)
@@ -25,17 +27,30 @@ public class LabelNode: AnyNode {
2527 l. font = font
2628 l. text = string
2729 }
30+ l. numberOfLines = numberOfLines
2831 return l
2932 }
3033 }
3134
3235 public override func sizeThatFits( size: CGSize ) -> CGSize {
3336
37+ struct Cache {
38+ static let label = UILabel ( )
39+ }
40+
41+ Cache . label. attributedText = nil
42+ Cache . label. text = nil
43+ Cache . label. font = nil
44+ Cache . label. numberOfLines = numberOfLines
45+
3446 switch text {
3547 case . Attributed( let attrString) :
36- return attrString. boundingRectWithSize ( size, options: [ . UsesLineFragmentOrigin, . UsesFontLeading] , context: nil ) . size
48+ Cache . label. attributedText = attrString
49+ return Cache . label. sizeThatFits ( size)
3750 case . Regular( let string, let font) :
38- return ( string as NSString ) . boundingRectWithSize ( size, options: [ . UsesLineFragmentOrigin, . UsesFontLeading] , attributes: [ NSFontAttributeName: font] , context: nil ) . size
51+ Cache . label. text = string
52+ Cache . label. font = font
53+ return Cache . label. sizeThatFits ( size)
3954 }
4055 }
4156}
0 commit comments