1

我可以轻松地使用TTTAttributedlabel可点击的 url、名称等。

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:{ }]
4

2 回答 2

0

您可以轻松地制作组件。提供视图,实现computeLayoutThatFits:. CKImageComponent 就是一个例子。

但是在文字的情况下。由于布局大小,这可能很难。一旦你TTTAttributedLabel用作文本的渲染,你必须手动提供文本大小。这绝对不是你想要的。

如您所见,CKLabelComponent 的父级CKTextComponent 被实现为ComponentKit 的子项目。它处理布局大小、文本渲染、文本布局缓存。(它是用 TextKit 实现的。)如果你想使用TTTAttributedLabel,你必须用 TTTAttributedLabel 自己处理所有事情。它可能会减慢您的滚动速度,因为 CKTextComponent 实现异步渲染但 TTTAttributedLabel 没有。


CKTextKitEntityAttributeName可能实现你的目标

于 2017-05-21T18:18:00.820 回答
0

我的方法是将 CK 与 TTTAttributedLabel 结合起来。首先,我定义了一个结构,它包含一些我需要设置为 TTTAttributedLabel 的基本信息,

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

然后,我创建一个新类,命名为 CKComponent 的子类 ABCKAttributeLabelComponent,实现如下初始化方法:

+ (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

第三,计算 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。

它可能不是很优雅,但它可以工作。

于 2017-08-08T16:03:49.003 回答