2016-05-06 2 views
1

にTTTAttributedLabelのようなラベルを起因の作成などiOSのは、私は簡単にタップ可能なURL、名前を持っている<code>TTTAttributedlabel</code>を使用することができますCKLabelComponent

1)どのように私はCKLabelComponentと同様のものを作成することができますか?

2)CKTextComponentを使用する必要がありますか?

[CKLabelComponent newWithLabelAttributes:{ 
           .string = @"This shall be context This shall be context This shall be context This shall be context", 
           .font = [UIFont fontWithName:@"Baskerville" size:14] 
          } 
          viewAttributes:{ 
           {@selector(setBackgroundColor:), [UIColor clearColor]}, 
           {@selector(setUserInteractionEnabled:), @NO}, 
          } 
          size:{ }] 
+0

あなたはそれについて解決策を見つけましたか? – lee

+1

いいえ。彼らはよくサポートしていないし、私はそれを使用してあきらめます。彼らは将来的にはより多くの支持を得ることを願っています。 –

答えて

0

コンポーネントを簡単に作成できます。ビューを提供し、computeLayoutThatFits:を実装します。 CKImageComponentは例です。

しかし、テキストの状況で。レイアウトのサイズのため、難しいかもしれません。 TTTAttributedLabelをテキストのレンダリングとして使用すると、テキストサイズを手動で入力する必要があります。これは間違いなくあなたが望むものではありません。

ご覧のとおり、CKLabelComponentの親であるCKTextComponentは、ComponentKitのサブプロジェクトとして実装されています。レイアウトサイズ、テキストレンダリング、テキストレイアウトキャッシュを処理します。 (TextKitで実装されています)TTTAttributedLabelを使用する場合は、TTTAttributedLabelを使用してすべてを処理する必要があります。 CKTextComponentは非同期レンダーを実装しますが、TTTAttributedLabelは実装しないため、スクロールが遅くなる可能性があります。


CKTextKitEntityAttributeName

0

私のやり方がTTTAttributedLabelでCKを組み合わせることであるあなたの目標を達成することができます。

+ (instancetype)newWithData:(ABCKAttributeLabelData)data { 
ABCKAttributeLabelComponent *com = 
[super newWithView:{ 
    [TTTAttributedLabel class], 
    { 
     {@selector(setText:), [data.content attributedStringWithStyle: 
     @{NSForegroundColorAttributeName : data.normalColor, 
        NSFontAttributeName : data.normalFont,}]}, 
     {@selector(setLinkAttributes:), @{ NSForegroundColorAttributeName : data.linkColor, 
              NSFontAttributeName : data.linkFont}}, 
     {@selector(setActiveLinkAttributes:), @{ NSForegroundColorAttributeName : data.linkColor, 
               NSFontAttributeName : data.linkFont}}, 
     {@selector(setNumberOfLines:), data.numberOfLines ?: 0}, 
     {@selector(setLineBreakMode:), NSLineBreakByTruncatingTail}, 
    } 
} size:{}]; 

com.attributeString = [data.content attributedStringWithStyle: 
     @{NSForegroundColorAttributeName : data.normalColor, 
        NSFontAttributeName : data.normalFont,}]; 
com.normalFont = data.normalFont; 
com.numOfLine = data.numberOfLines ?: 0; 

return com 
:まず、私は以下のようにCKComponentのABCKAttributeLabelComponentサブクラスという名前の新しいクラス、実装initializeメソッドを作成し、その後

struct ABCKAttributeLabelData { 
    NSString *content; 
    UIFont *normalFont; 
    UIColor *normalColor; 
    UIFont *linkFont; 
    UIColor *linkColor; 
    NSInteger numberOfLines; 
}; 

、それは私がTTTAttributedLabelに設定する必要がありますいくつかの基本的な情報が含まれている、構造体を定義します

第3に、TTTAttributedLabelのサイズを計算して返します。 CKはcomputeLayoutThatFits:メソッドを呼び出してコンポーネントのサイズを取得します。だからそれを上書きしてください。

- (CKComponentLayout)computeLayoutThatFits:(CKSizeRange)constrainedSize { 
// self.attributeString,self.numOfLine and self.normalFont I saved as property in initialize method 
CGSize computerSize = [self.attributeString sizeLabelToFitToSize:constrainedSize.max numberLines:self.numOfLine font:self.normalFont]; 
return { 
    self, 
    constrainedSize.clamp({ 
     CKCeilPixelValue(computerSize.width), 
     CKCeilPixelValue(computerSize.height) 
    }), 
    {} 
}; 

}

物事の残りの部分はABCKAttributeLabelComponentを使用することです。

非常に優雅ではないかもしれませんが、動作します。

関連する問題