1. 先說一下大眾需求,如圖
1.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 問題來了侧啼,這樣顯示只能實現(xiàn)底部字體持平,并不能使縮小的字居中顯示堪簿,據(jù)目前的技術來講痊乾,只能自定義一個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));
// 網(wǎng)絡請求回來,設置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;
}
}
大致思路是湿滓,先創(chuàng)建一個view,上面加兩個label舌狗,label的文字從后臺獲取叽奥,當取到文字之后,重新設置view的寬度痛侍,為什么要重新設置view寬度呢朝氓,因為這里的文字有邊框,效果如圖1.2
圖1.2