1. 先说一下大众需求,如图
此种需求利用label的富文本设置一下不同字号就ok,代码如下
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 300, 80)];
label.backgroundColor = [UIColor lightGrayColor];
//初始化富文本对象:
NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:@"你好我好大家好"];
//给富文本添加属性1-字体大小
[attributedStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:16.0]
range:NSMakeRange(2, 2)];
效果如图1.1
1.2 问题来了,这样显示只能实现底部字体持平,并不能使缩小的字居中显示,据目前的技术来讲,只能自定义一个view,如果有好的方法,希望广告友人给与指正,下面提供一个自定义view
_changyouView = [[UIView alloc] init];
[self addSubview:_changyouView];
[_changyouView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(_headerImageV.mas_right).offset(kAppAdaptWidth(20));
make.top.mas_equalTo(_integralLabel.mas_bottom).offset(kAppAdaptHeight(8));
make.height.mas_equalTo(kAppAdaptHeight(18));
make.width.mas_equalTo(kAppAdaptWidth(60));
}];
_changyouView.layer.cornerRadius = kAppAdaptHeight(8);
_changyouView.layer.masksToBounds = YES;
_changyouView.layer.borderColor = kHRGB(0xCCA969).CGColor;
_changyouView.layer.borderWidth = kAppAdaptHeight(1.0);
_totalLabel = [[UILabel alloc] init];
[_changyouView addSubview:_totalLabel];
[_totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(_changyouView.mas_left).offset(kAppAdaptWidth(5));
make.top.mas_equalTo(_changyouView.mas_top).offset(0);
make.height.mas_equalTo(kAppAdaptHeight(19));
make.width.mas_equalTo(kAppAdaptWidth(37));
}];
_totalLabel.text = kStr(@"Total earnings");
_totalLabel.font = kAppFont(kAppAdaptWidth(8));
_totalLabel.textColor = kHRGB(0xCCA969);
_totalLabel.textAlignment = NSTextAlignmentCenter;
_changyouLabel = [[UILabel alloc] init];
[_changyouView addSubview:_changyouLabel];
[_changyouLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(_totalLabel.mas_right).offset(kAppAdaptWidth(2));
make.top.mas_equalTo(_changyouView.mas_top).offset(kAppAdaptHeight(0));
make.height.mas_equalTo(kAppAdaptHeight(19));
}];
_changyouLabel.textColor = kHRGB(0xCCA969);
_changyouLabel.font = kAppFont(kAppAdaptHeight(14));
// 网络请求回来,设置label,重新设置view的尺寸
- (void)setupViewWithIntagral:(JFIntegral *)integral showInteface:(BOOL)showInterface {
NSString *integralStr = [NSString stringWithFormat:@"%@ 畅由积分",points];
_integralLabel.text = integralStr;
[_integralLabel setPartString:@"畅由积分" attributes:@{NSFontAttributeName:kAppFont(kAppAdaptWidth(12))}];
_changyouLabel.text = integral.homeTotalProfits;
[_changyouView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(_headerImageV.mas_right).offset(kAppAdaptWidth(20));
make.top.mas_equalTo(_integralLabel.mas_bottom).offset(kAppAdaptHeight(8));
make.height.mas_equalTo(kAppAdaptHeight(19));
make.right.mas_equalTo(_changyouLabel.mas_right).offset(kAppAdaptWidth(5));
}];
_changyouLabel.textAlignment = NSTextAlignmentCenter;
}
}
大致思路是,先创建一个view,上面加两个label,label的文字从后台获取,当取到文字之后,重新设置view的宽度,为什么要重新设置view宽度呢,因为这里的文字有边框,效果如图1.2