iOS在CKLabelComponent中创建属性标签,如TTtatAttributedLabel
我可以很容易地使用iOS在CKLabelComponent中创建属性标签,如TTtatAttributedLabel,ios,tttattributedlabel,componentkit,Ios,Tttattributedlabel,Componentkit,我可以很容易地使用TTTAttributedlabel来设置可点击的url、名称等 1) 如何创建类似于CKLabelComponent中的内容 [CKLabelComponent newWithLabelAttributes:{ .string = @"This shall be context This shall be context This shall be context This shall be context",
TTTAttributedlabel
来设置可点击的url、名称等
1) 如何创建类似于CKLabelComponent
中的内容
[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:{ }]
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:{ }]
您可以轻松地创建组件。提供视图,实现符合以下条件的
计算布局:
。CKImageComponent就是一个例子
但是在文本的情况下。这可能很难,因为布局的大小。使用TTTAttributedLabel
作为文本呈现后,必须手动提供文本大小。这绝对不是你想要的
如您所见,CKLabelComponent的父级CKTextComponent作为ComponentKit的子项目实现。它处理布局大小、文本呈现、文本布局缓存。(它是用TextKit实现的。)如果你想使用TTTAttributedLabel
,你必须用TTTAttributedLabel自己处理所有事情。它可能会减慢您的滚动速度,因为CKTextComponent实现异步渲染,而TTTAttributedLabel不实现
cktextKitenityAttributeName
可以实现您的目标您可以轻松制作组件。提供视图,实现符合以下条件的计算布局:
。CKImageComponent就是一个例子
但是在文本的情况下。这可能很难,因为布局的大小。使用TTTAttributedLabel
作为文本呈现后,必须手动提供文本大小。这绝对不是你想要的
如您所见,CKLabelComponent的父级CKTextComponent作为ComponentKit的子项目实现。它处理布局大小、文本呈现、文本布局缓存。(它是用TextKit实现的。)如果你想使用TTTAttributedLabel
,你必须用TTTAttributedLabel自己处理所有事情。它可能会减慢您的滚动速度,因为CKTextComponent实现异步渲染,而TTTAttributedLabel不实现
cktextKitenityAttributeName
可能会实现您的目标我的方法是将CK与TTTAttributedLabel结合起来。首先,我定义了一个结构,它包含一些我需要设置为TTtatAttributedLabel的基本信息
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
第三,计算TTtatAttributedLabel的大小和返回值。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
它可能不是很优雅,但它可以工作。我的方法是将CK与tttatAttributedLabel结合起来。首先,我定义了一个结构,它包含一些我需要设置为TTtatAttributedLabel的基本信息
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
第三,计算TTtatAttributedLabel的大小和返回值。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
它可能不是很优雅,但可以工作。您找到解决方案了吗?没有。他们没有很好的支持,我放弃了使用它。希望他们将来会有更多的支持。你找到解决方案了吗?没有。他们没有很好的支持,我放弃了使用。希望他们将来能得到更多的支持。