OC代码:
-(void)changeTopics:(NSArray *)topics{
_topicsArr = [NSMutableArray arrayWithArray:topics];
CGSize size = CGSizeMake(320,2000); //设置一个行高上限
if ([_shortTextView.topicView subviews]) {
for (UIView *view in [_shortTextView.topicView subviews])
{
if ([view isKindOfClass:[UIView class]])
{
[view removeFromSuperview];
}
}
}
if (topics.count) {
UILabel *lbl1 = [[UILabel alloc]init];
NSHotTopicsScrollModel *scrollModel = topics[0];
[lbl1 setText:scrollModel.name];
lbl1.font = [UIFont fontWithName:@"PingFangSC" size:12];
NSDictionary *lbl1Attribute = @{NSFontAttributeName: lbl1.font};
CGSize lbl1Size = [lbl1.text boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:lbl1Attribute context:nil].size;
lbl1.textColor = [UIColor whiteColor];
lbl1.textAlignment = NSTextAlignmentCenter;
lbl1.backgroundColor = BACK_COLOR1;
lbl1.layer.cornerRadius = 3;
lbl1.layer.masksToBounds = YES;
[_shortTextView.topicView addSubview:lbl1];
[lbl1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(lbl1Size.width+6, 21));
make.right.equalTo(@(-10));
make.top.equalTo(@0);
}];
if (topics.count == 2) {
UILabel *lbl2 = [[UILabel alloc]init];
NSHotTopicsScrollModel *scrollModel = topics[1];
[lbl2 setText:scrollModel.name];
lbl2.font = [UIFont fontWithName:@"PingFangSC" size:12];
NSDictionary *lbl2Attribute = @{NSFontAttributeName: lbl2.font};
CGSize lbl2Size = [lbl2.text boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:lbl2Attribute context:nil].size;
lbl2.textColor = [UIColor whiteColor];
lbl2.textAlignment = NSTextAlignmentCenter;
lbl2.backgroundColor = BACK_COLOR1;
lbl2.layer.cornerRadius = 1;
lbl2.layer.masksToBounds = YES;
[_shortTextView.topicView addSubview:lbl2];
[lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(lbl2Size.width+6, 21));
make.right.equalTo(@(-(20+lbl1Size.width+6)));
make.top.equalTo(@0);
}];
}
}
}
JS代码:
defineClass('ShortTextViewController', {
changeTopics: function(topics) {
var _topicsArr = self.valueForKey("_topicsArr");
var _shortTextView = self.valueForKey("_shortTextView");
_topicsArr = NSMutableArray.arrayWithArray(topics);
var size = {width:320,height:2000}; //设置一个行高上限
var count = _shortTextView.topicView().subviews().count();
for (var i = 0; i < count; i++) {
_shortTextView.topicView().removeAllSubviews();
}
if (topics.count()) {
var lbl1 = UILabel.alloc().init();
var scrollModel = topics.objectAtIndex(0);
lbl1.setText(scrollModel.name());
lbl1.setFont(UIFont.fontWithName_size("PingFangSC", 12));
var lbl1Attribute = {
NSFontAttributeName: lbl1.font()
};
var NSStringDrawingUsesLineFragmentOrigin = 1 << 0;
var lbl1Size = lbl1.text().boundingRectWithSize_options_attributes_context(size, NSStringDrawingUsesLineFragmentOrigin, lbl1Attribute, null);
lbl1.setTextColor(UIColor.whiteColor());
lbl1.setTextAlignment(1);
lbl1.setBackgroundColor(UIColor.colorWithHexString("#c4c4c4"));
lbl1.layer().setCornerRadius(1);
lbl1.layer().setMasksToBounds(YES);
_shortTextView.topicView().addSubview(lbl1);
lbl1.mas__makeConstraints(block('MASConstraintMaker*', function(make) {
make.width().equalTo()(lbl1Size.width + 6);
make.height().equalTo()(21);
make.right().equalTo()(-10);
make.top().equalTo()(0);
}));
if (topics.count() == 2) {
var lbl2 = UILabel.alloc().init();
var scrollModel = topics.objectAtIndex(1);
lbl2.setText(scrollModel.name());
lbl2.setFont(UIFont.fontWithName_size("PingFangSC", 12));
var lbl2Attribute = {
NSFontAttributeName: lbl2.font()
};
var lbl2Size = lbl2.text().boundingRectWithSize_options_attributes_context(size, NSStringDrawingUsesLineFragmentOrigin, lbl2Attribute, null);
lbl2.setTextColor(UIColor.whiteColor());
lbl2.setTextAlignment(1);
lbl2.setBackgroundColor(UIColor.colorWithHexString("#c4c4c4"));
lbl2.layer().setCornerRadius(1);
lbl2.layer().setMasksToBounds(YES);
_shortTextView.topicView().addSubview(lbl2);
lbl2.mas__makeConstraints(block('MASConstraintMaker*', function(make) {
make.width().equalTo()(lbl2Size.width+6);
make.height().equalTo()(21);
make.right().equalTo()(-(20 + lbl1Size.width + 6));
make.top().equalTo()(0);
}));
}
}
},
});
总结:
1.注意for in循环和for循环的区别,for in循环是一次性取出所有的元素进行操作,而for循环是在每次循环时重新取出元素,取出的值可能和在for循环中是不一样的。
2.对于类似于1 << 0的枚举值,可以先声明一个变量赋值var NSStringDrawingUsesLineFragmentOrigin = 1 << 0;然后再取值。
3.OC中的boundingRectWithSize函数,在JS中去掉结尾的.size。
网友评论