ios 富文本不能解决的事情

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 问题来了,这样显示只能实现底部字体持平,并不能使缩小的字居中显示,据目前的技术来讲,只能自定义一个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

图1.2
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,302评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,241评论 4 61
  • 不知不觉就到了月底,日子就这样似水一样的流走了,猛然惊醒又一年马上就过去了。回首这一年,感觉忙忙碌碌最后又不知道自...
    源小米阅读 357评论 0 0
  • 以前的我一直都是一个只追求精神满足的人,在我看来只要快乐,钱并不重要,我一直都有一个观点:钱多有钱多的活法,钱少有...
    longlong8612009阅读 349评论 1 0
  • 随着网络带宽的日益增加和便携式设备,如智能手机或平板电脑处理能力的增强,基于互联网的实时通信已经成为热点。 虽然视...
    befoio阅读 2,328评论 0 5