自动布局,防京东商品属性,Label自适应


效果如上图所示
//写到获取信息后的请求完成里面
//数据示例*********
{
    Latas =     (
        330ML,
        500ML
    );
    Marca =     (
        Refriango
    );
}
********************/

for ( UIView *view in _selectScrollView . subviews ) {
               
if (view != _nameLabel && view != logoImageView && view != priceLabel && view != line_scroll ) {
                    [view
removeFromSuperview ];
                }
            }
           
selectYES = [ NSMutableArray arrayWithArray :[[ _dataDict objectForKey : @"attr" ] componentsSeparatedByString : @"," ]];
           
_moreHeight = 120 ;
           
int j = 0 ;
           
int k = 1 ;
           
int l = 0 ;
           
//BOOL _default = NO;
           
for ( NSString *key in details) {
               
NSMutableArray *arr = [ NSMutableArray arrayWithArray :[details objectForKey :key]];
           
  j =
0 ;
                for ( int i= 0 ; i<arr. count ; i++) {
                    NSString *str = [ NSString stringWithFormat : @"%@" ,[arr objectAtIndex :i]];
                   
NSDictionary *dictSize = [ NSDictionary dictionaryWithObjectsAndKeys :[ UIFont systemFontOfSize : 14.0 ], NSFontAttributeName , nil ];
                   
CGSize size = [str boundingRectWithSize : CGSizeMake ( _selectScrollView . bounds . size . width - 100 , 20 ) options : NSStringDrawingUsesLineFragmentOrigin attributes :dictSize context : nil ]. size ;
                   
                   
UILabel *label = [[ UILabel alloc ] init ];
                    label.
font = [ UIFont systemFontOfSize : 14.0 ];
                    label.
textAlignment = NSTextAlignmentCenter ;
                    label.
layer . cornerRadius = 2.0 ;
                    label.
layer . masksToBounds = NO ;
                    label.
textColor = [ UIColor blackColor ];
                    label.
layer . borderWidth = 0.5 ;
                    label.
tag = 1000 *k + i;
                    label.
layer . borderColor = ColorWihtRGB ( 175 , 175 , 175 , 1.0 ). CGColor ;
                   
if (i == 0 ) {
                        label.
frame = CGRectMake ( 10 , _moreHeight + 10 +l* 10 , size. width + 10 , 20 );
                       
_prevWith = size. width + 20 ;
                       
_moreThanWideHeight = _moreHeight + 10 ;
                    }
else {
                        label.
frame = CGRectMake ( _prevWith + 10 , _moreThanWideHeight +l* 10 , size. width + 20 , 20 );
                       
_prevWith += size. width + 30 ;
                    }
                   
                   
if ( _prevWith > _selectScrollView . bounds . size . width - 20 ) {
                       
_prevWith = 10 ;
                        label.
frame = CGRectMake ( _prevWith , _moreThanWideHeight + 30 +l* 10 , size. width + 20 , 20 );
                       
_prevWith = size. width + 30 ;
                       
_moreThanWideHeight += 30 ;
                       
//_moreHeight = _moreThanWideHeight;
                        j++;
                    }
                    label.
text = str;
                    [
_selectScrollView addSubview :label];
                   
                   
UITapGestureRecognizer *tap = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (attributeSelect:)];
                    label.
userInteractionEnabled = YES ;
                    [label
addGestureRecognizer :tap];
                   
                   
for ( NSString *str in selectYES ) {
                       
if ([str isEqualToString :label. text ]) {
                            label.
layer . borderColor = ColorWihtRGB ( 240 , 75 , 75 , 1.0 ). CGColor ;
                           
break ;
                        }
                    }
                }
               
UILabel *nameLabel = [[ UILabel alloc ] initWithFrame : CGRectMake ( 10 , _moreHeight - 25 +l* 10 , _selectScrollView . bounds . size . width - 20 , 40 )];
                nameLabel.
text = key;
                nameLabel.
font = [ UIFont systemFontOfSize : 14.0 ];
                [
_selectScrollView addSubview :nameLabel];
               
_moreHeight += 40 +j* 30 ;
                k++;
                l++;
            }

           
UILabel *nameLabel = [[ UILabel alloc ] initWithFrame : CGRectMake ( 10 , _moreHeight +l* 10 - 5 , _selectScrollView . bounds . size . width - 20 , 20 )];
            nameLabel.
text = NSLocalizedString ( @" 数量 " , "Localizable" );
            nameLabel.
font = [ UIFont systemFontOfSize : 14.0 ];
            [
_selectScrollView addSubview :nameLabel];
           
           
bgView_num . frame = CGRectMake ( 10 , _moreHeight + 20 +l* 10 , 93 , 28 );
            [
_selectScrollView addSubview : bgView_num ];
           
            _selectScrollView.contentSize = CGSizeMake(screenWidth-50, _moreThanWideHeight+30+30+30+l*10);



//点击切换产品
//_propertyDict  产品关联字典 数据示例************
"details":{
"246":"Refriango,500ML",
"243":"Refriango,330ML"
}
*************************/
- ( void )attributeSelect:( UITapGestureRecognizer *)tap {

  selectYES = [[ NSMutableArray alloc ] init ];
       
for ( UILabel *label in _selectScrollView . subviews ) {
           
if ([label isKindOfClass :[ UILabel class ]]) {
               
//NSLog(@"%@",label.layer.borderColor);
                label.
textColor = [ UIColor blackColor ];
               
if (label. tag / 1000 == tap. view . tag / 1000 ) {
                   
if (label. tag == tap. view . tag ) {
                       
NSLog ( @"111  == %ld" ,label.tag);
                        label.
layer . borderColor = ColorWihtRGB ( 240 , 75 , 75 , 1.0 ). CGColor ;
                        [
selectYES addObject :label. text ];
                    }
else {
                       
if (label. layer . borderColor == ColorWihtRGB ( 240 , 75 , 75 , 1.0 ). CGColor ) {
                            [
selectYES addObject :label. text ];
                        }
else {
                           
NSLog ( @"222 ==  %ld" ,label.tag);
                            label.
layer . borderColor = ColorWihtRGB ( 175 , 175 , 175 , 1.0 ). CGColor ;
                        }
                    }
                }
else {
                   
if (label. layer . borderColor == ColorWihtRGB ( 240 , 75 , 75 , 1.0 ). CGColor ) {
                        [
selectYES addObject :label. text ];
                    }
else {
                       
NSLog ( @"222 ==  %ld" ,label.tag);
                       
//label.layer.borderColor = ColorWihtRGB(175, 175, 175, 1.0).CGColor;
                    }
                }
            }
        }
       
       
BOOL _select = NO ;
       
for ( NSString *key11 in _propertyDict ) {
           
NSArray *arr = [ NSMutableArray arrayWithArray :[[ _propertyDict objectForKey :key11] componentsSeparatedByString : @"," ]];
           
NSPredicate *filterPredicate = [ NSPredicate predicateWithFormat : @"NOT (SELF IN %@)" ,arr];
           
// 过滤数组
           
NSArray *reslutFilteredArray = [ selectYES filteredArrayUsingPredicate :filterPredicate];
           
           
if (reslutFilteredArray. count == 0 ) {
               
if ([key11 integerValue ] == [ self . good_id integerValue ]) {
                    [
buyToCar setBackgroundColor :[ UIColor colorWithRed : 240 / 255.0 green : 75 / 255.0 blue : 75 / 255.0 alpha : 1.0 ]];
                   
buyToCar . selected = YES ;
                   
return ;
                }
               
self . good_id = key11;
                _select =
YES ;
                [
buyToCar setBackgroundColor :[ UIColor colorWithRed : 240 / 255.0 green : 75 / 255.0 blue : 75 / 255.0 alpha : 1.0 ]];
               
buyToCar . selected = YES ;
               
break ;
            }
else {
                [
buyToCar setBackgroundColor :[ UIColor colorWithRed : 175 / 255.0 green : 175 / 255.0 blue : 175 / 255.0 alpha : 1.0 ]];
               
buyToCar . selected = NO ;
            }
        }
       
if (_select) {
           
//NSLog(@"%@",good_id);
            [
self good_info ];//获取商品详情的接口调用
        } 
else {
            [
buyToCar setBackgroundColor :[ UIColor colorWithRed : 175 / 255.0 green : 175 / 255.0 blue : 175 / 255.0 alpha : 1.0 ]];
            buyToCar.selected = NO;
   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值