绘制长度不定可自动换行的标签

#pragma mark - 尝试绘制自动换行的类型标签label

- (void)createTypeBtnsView{
    
    tagView = [[UIView alloc]initWithFrame:CGRectMake(10, 45 + 300*self.model.imagesArray.count, [DMDevceManager screenWidth]-20, 10)];
    CGFloat positionX = 0.0;
    CGFloat positionY = 0.0;
    CGFloat bgViewWidth = [DMDevceManager screenWidth]-20;
    
    UIFont *btnTitleFont = [UIFont systemFontOfSize:13];
    
    //如果有图片作为边框,使用注释
//    UIImage *selImage = [UIImage imageNamed:@"greenCir1"];
//    UIImage *norImage = [UIImage imageNamed:@"grayCir2"];
    
//    CGFloat left = selImage.size.width/4;
//    CGFloat top = selImage.size.height/4;
    
    for(int i = 0;i<self.model.typesArray.count;i++)
    {
        CGFloat btnWidth = [DMUtils textWidth:self.model.typesArray[i] Font:btnTitleFont height:25];
        
        if(positionX + btnWidth > bgViewWidth){
            positionX = 0;
            positionY += 30;
        }
        
        UIButton *btn = [DMUITool createButtonWithFrame:CGRectMake(positionX, positionY, btnWidth, 25) title:self.model.typesArray[i] target:self action:@selector(typeBtnClick:) tag:i+30000];
        btn.titleLabel.font = btnTitleFont;
        
        btn.backgroundColor = Color(103, 206, 249);
        btn.layer.masksToBounds = YES;
        btn.userInteractionEnabled = YES;
        btn.layer.cornerRadius = 12;
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        
        positionX += (btnWidth+5);
        //如果有图片作为边框,从4分之1处拉伸
//        [btn setBackgroundImage:[selImage stretchableImageWithLeftCapWidth:left topCapHeight:top] forState:UIControlStateSelected];
//        [btn setBackgroundImage:[norImage stretchableImageWithLeftCapWidth:left topCapHeight:top] forState:UIControlStateNormal];
        
        [tagView addSubview:btn];
        
    }
/*
 *获取文字所占宽度
 *@param text 文本内容,计算式包括了换行空格等
 *@param font 字体
 *@param height:指定高度下计算,若不设限使用CGFLOAT_MAX
 */
+ (CGFloat)textWidth:(NSString *)text Font:(UIFont *)font height:(CGFloat)height
{
    if(![DMDevceManager isiOS7]){
        return [text sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, height) lineBreakMode:NSLineBreakByCharWrapping].width;
        
    }else{
    
//        NSDictionary *attribute = @{NSFontAttributeName:font};
//        CGSize retSize = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:   NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
//        
//        return retSize.width;
        
        if(text.length <= 0){
            return 0;
        }
        
    UITextView *textView = APPDELEGATE.textView;
    textView.text = text;
    textView.font = font;
    CGSize size = [textView sizeThatFits:CGSizeMake(CGFLOAT_MAX, height)];
    return size.width;
        
    }
}



效果如图:

172902_alHB_2526171.png

转载于:https://my.oschina.net/bjwkk/blog/653017

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值