My way is to combine CK with TTTAttributedLabel. First, I define a struct, it contains some basic info I need to set to TTTAttributedLabel,
struct ABCKAttributeLabelData {
NSString *content;
UIFont *normalFont;
UIColor *normalColor;
UIFont *linkFont;
UIColor *linkColor;
NSInteger numberOfLines;
};
Then, I create a new class, named ABCKAttributeLabelComponent subclass of CKComponent, implemented initialize method as below:
+ (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
Third, calculate TTTAttributedLabel's size and return. CK will call computeLayoutThatFits: method to get component size. So overwrite it.
- (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)
}),
{}
};
}
The rest of the things is to use ABCKAttributeLabelComponent.
it may not be very elegance, but it can works.