美文网首页
UIViewController里的控件自适应的时候,修改其他控

UIViewController里的控件自适应的时候,修改其他控

作者: liuzhongyi | 来源:发表于2018-06-22 15:36 被阅读6次

    #import "TTeamInformationVC.h"

    #import "TSessionTeamMemberCell.h"

    #import "TMemberListTeamVC.h"

    @interface TTeamInformationVC ()

    @property (nonatomic, strong) UITextView *teamTitleTF;

    @property (nonatomic, strong) UITextView *teamContentTF;

    @property (nonatomic, strong) UILabel *memberLabel;

    @property (nonatomic, strong) UIView *bottomView;

    @property (nonatomic, strong) NSMutableArray *dataSource;

    @end

    @implementationTTeamInformationVC

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

        [self setupUIAction];

    }

    - (void)viewDidLayoutSubviews{

        [super viewDidLayoutSubviews];

        _bottomView.mj_y = CGRectGetMaxY(_teamContentTF.frame);

    }

    #pragma mark -- 界面布局方法

    - (void)setupUIAction{

        self.pageTitle=self.title=@"群资料";

        self.view.backgroundColor = SharedColor.themeGrey;

        _dataSource = [NSMutableArray array];

        //如果是群主

        [self installOKOnlyText:@"修改"];

        UIView*whiteView = [UIViewnew];

        whiteView.backgroundColor = [UIColor whiteColor];

        [self.viewaddSubview:whiteView];

        UILabel*teamTitleLabel = [UILabelnew];

        teamTitleLabel.textColor = [UIColor blackColor];

        teamTitleLabel.text=@"群名称";

        teamTitleLabel.font= [UIFontsystemFontOfSize:15];

        [whiteViewaddSubview:teamTitleLabel];

        [teamTitleLabelmas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(whiteView.mas_top);

            make.left.equalTo(whiteView.mas_left).offset(15);

            make.height.equalTo(@40);

        }];

        _teamTitleTF = [UITextView new];

        _teamTitleTF.textColor = [UIColor colorWithWhite:0 alpha:0.7];

        _teamTitleTF.text = @"群名称";

        _teamTitleTF.font = [UIFont systemFontOfSize:15];

        _teamTitleTF.userInteractionEnabled = YES;//群主的时候打开

        _teamTitleTF.delegate = self;

        [whiteViewaddSubview:_teamTitleTF];

        [_teamTitleTF mas_makeConstraints:^(MASConstraintMaker *make) {

            make.left.equalTo(teamTitleLabel.mas_right).offset(30);

            make.right.equalTo(whiteView.mas_right).offset(-15);

            make.centerY.equalTo(teamTitleLabel.mas_centerY);

            make.height.equalTo(@40);

        }];

        UILabel*lineLabel = [UILabelnew];

        lineLabel.backgroundColor=RGBA(246,246,246,1.0);

        [whiteViewaddSubview:lineLabel];

        [lineLabelmas_makeConstraints:^(MASConstraintMaker *make) {

            make.leading.trailing.equalTo(whiteView);

            make.top.equalTo(teamTitleLabel.mas_bottom);

            make.height.equalTo(@1);

        }];

        UILabel*teamContentLabel = [UILabelnew];

        teamContentLabel.textColor= [UIColorblackColor];

        teamContentLabel.text=@"群介绍";

        teamContentLabel.font= [UIFontsystemFontOfSize:15];

        [whiteViewaddSubview:teamContentLabel];

        [teamContentLabelmas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(lineLabel.mas_bottom);

            make.left.equalTo(whiteView.mas_left).offset(15);

            make.height.equalTo(@40);

        }];

        _teamContentTF = [UITextView new];

        _teamContentTF.textColor = [UIColor colorWithWhite:0 alpha:0.7];

        _teamContentTF.text = @"群介绍内容";

        _teamContentTF.font = [UIFont systemFontOfSize:15];

        _teamContentTF.userInteractionEnabled = YES;//群主的时候打开

        _teamContentTF.delegate = self;

        [whiteViewaddSubview:_teamContentTF];

        [_teamContentTF mas_makeConstraints:^(MASConstraintMaker *make) {

            make.left.equalTo(teamContentLabel.mas_right).offset(30);

            make.right.equalTo(whiteView.mas_right).offset(-15);

            make.top.equalTo(lineLabel.mas_bottom);

            make.height.equalTo(@40);

        }];

        _bottomView = [UIView new];

        _bottomView.backgroundColor = [UIColor whiteColor];

        [whiteViewaddSubview:_bottomView];

        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(teamContentLabel.mas_bottom);

            make.leading.trailing.equalTo(whiteView);

            make.height.equalTo(@145);

        }];

        UIView*lineLabelTwo = [UIViewnew];

        lineLabelTwo.backgroundColor=RGBA(246,246,246,1.0);

        [_bottomViewaddSubview:lineLabelTwo];

        [lineLabelTwomas_makeConstraints:^(MASConstraintMaker *make) {

            make.leading.trailing.equalTo(whiteView);

            make.top.equalTo(_bottomView.mas_top);

            make.height.equalTo(@5);

        }];

        _memberLabel = [UILabel new];

        _memberLabel.textColor = [UIColor blackColor];

        _memberLabel.text = @"群成员(0人)";

        _memberLabel.font = [UIFont systemFontOfSize:15];

        [_bottomView addSubview:_memberLabel];

        [_memberLabel mas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(lineLabelTwo.mas_bottom);

            make.left.equalTo(_bottomView.mas_left).offset(15);

            make.height.equalTo(@40);

        }];

        UIButton *allBtn = [UIButton new];

        [allBtnsetTitle:@"全部 >" forState:UIControlStateNormal];

        [allBtnsetTitleColor:[UIColor colorWithWhite:0 alpha:0.7] forState:UIControlStateNormal];

        [allBtnaddTarget:self action:@selector(allBtnClickAction:) forControlEvents:UIControlEventTouchUpInside];

        [_bottomViewaddSubview:allBtn];

        [allBtnmas_makeConstraints:^(MASConstraintMaker *make) {

            make.right.equalTo(_bottomView.mas_right).offset(-15);

            make.height.equalTo(@40);

            make.centerY.equalTo(_memberLabel.mas_centerY);

        }];

        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

        UICollectionView *memberCollection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];

        memberCollection.backgroundColor = [UIColor whiteColor];

        memberCollection.delegate=self;

        memberCollection.dataSource=self;

        [memberCollectionregisterClass:[TSessionTeamMemberCell class] forCellWithReuseIdentifier:T_SessionTeam_Member_Cell];

        [_bottomViewaddSubview:memberCollection];

        CGFloatspacingF;

        if (kScreenWidth > 320) {

            //4寸以上的手机

            spacingF = (kScreenWidth-50*5) /6.0;

        }else{

            //4寸手机

            spacingF = (kScreenWidth-50*4) /5.0;

        }

        [memberCollectionmas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(_memberLabel.mas_bottom);

            make.height.equalTo(@100);

            make.left.equalTo(_bottomView.mas_left).offset(spacingF);

            make.right.equalTo(_bottomView.mas_right).offset(-spacingF);

        }];

        [whiteViewmas_makeConstraints:^(MASConstraintMaker *make) {

            make.leading.trailing.equalTo(self.view);

            make.top.equalTo(self.view.mas_top).offset(1);

            make.bottom.equalTo(_bottomView.mas_bottom);

        }];

        //如果不是契约群

        UIButton*outTeamBtn = [UIButtonnew];

        //群成员

        [outTeamBtnsetTitle:@"退出该群"forState:UIControlStateNormal];

        //群主

    //    [outTeamBtn setTitle:@"解散该群" forState:UIControlStateNormal];

        [outTeamBtnsetTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        [outTeamBtnsetBackgroundColor:[UIColor redColor]];

        [outTeamBtnaddTarget:self action:@selector(outTeamBtnClickAction:) forControlEvents:UIControlEventTouchUpInside];

        [self.viewaddSubview:outTeamBtn];

        //契约群隐藏

    //    outTeamBtn.hidden = YES;

        [outTeamBtnmas_makeConstraints:^(MASConstraintMaker *make) {

            make.leading.trailing.equalTo(self.view);

            make.height.equalTo(@50);

            make.bottom.equalTo(self.view.mas_bottom).offset(49 - TABBAR_HEIGHT);

        }];

        //kvo监听群介绍

        [_teamContentTF addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];

    }

    #pragma mark -- 全部按钮点击方法

    - (void)allBtnClickAction:(UIButton*)sender{

        TMemberListTeamVC *vc = [[TMemberListTeamVC alloc]init];

        [self.navigationController pushViewController:vc animated:YES];

    }

    #pragma mark -- 底部按钮点击方法

    - (void)outTeamBtnClickAction:(UIButton*)sender{

    }

    #pragma mark -- 输入框的处理

    //kvo方法

    - (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context{

        //文字超过控件的时候修改控件的大小

        UITextView*view =(UITextView*)object;

        if([keyPathisEqualToString:@"contentSize"]) {

            view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.contentSize.height);

            //更改群介绍内容下面的控件约束,调用layout的方法

            //然后会在viewDidLayoutSubviews里实现具体的更新布局操作

            [self.view setNeedsLayout];

        }

    }

    #pragma mark -- UITextViewDelegate

    - (void)textViewDidChange:(UITextView*)textView{

        NSIntegerlimitNumber;

        if(textView ==_teamContentTF) {

            limitNumber =500;

        }else{

            if([textView.textisEqualToString:@""]) {

                self.isEabled=NO;

            }else{

                self.isEabled=YES;

            }

            limitNumber =10;

        }

        UITextRange*selectedRange = [textViewmarkedTextRange];

        //获取高亮部分

        UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];

        //如果在变化中是高亮部分在变,就不要计算字符了

        if(selectedRange && pos) {

            return;

        }

        NSString  *nsTextContent = textView.text;

        NSIntegerexistTextNum = nsTextContent.length;

        if(existTextNum > limitNumber){

            //截取到最大位置的字符(由于超出截部分在should时被处理了所在这里这了提高效率不再判断)

            NSString*s = [nsTextContentsubstringToIndex:limitNumber];

            if(textView ==_teamContentTF){

                _teamContentTF.text= s;

            }else{

                _teamTitleTF.text= s;

            }

        }

    }

    // 计算剩余可输入字数 超出最大可输入字数,就禁止输入

    - (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text{

        NSIntegerlimitNumber;

        if(textView ==_teamContentTF) {

            limitNumber =500;

        }else{

            limitNumber =10;

        }

        UITextRange*selectedRange = [textViewmarkedTextRange];

        //获取高亮部分

        UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];

        //获取高亮部分内容

        //NSString * selectedtext = [textView textInRange:selectedRange];

        //如果有高亮且当前字数开始位置小于最大限制时允许输入

        if(selectedRange && pos) {

            NSInteger startOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.start];

            NSInteger endOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.end];

            NSRangeoffsetRange =NSMakeRange(startOffset, endOffset - startOffset);

            if(offsetRange.location< limitNumber) {

                returnYES;

            }else{

                returnNO;

            }

        }

        NSString *comcatstr = [textView.text stringByReplacingCharactersInRange:range withString:text];

        NSIntegercaninputlen = limitNumber - comcatstr.length;

        if(caninputlen >=0){

            returnYES;

        }else{

            NSIntegerlen = text.length+ caninputlen;

            //防止当text.length + caninputlen < 0时,使得rg.length为一个非法最大正数出错

            NSRangerg = {0,MAX(len,0)};

            if(rg.length>0){

                // 计算表情的宽度

                NSString*s =@"";

                //判断是否只普通的字符或asc码(对于中文和表情返回NO)

                BOOL asc = [text canBeConvertedToEncoding:NSASCIIStringEncoding];

                if(asc) {

                    s = [textsubstringWithRange:rg];//因为是ascii码直接取就可以了不会错

                }else{

                    __blockNSIntegeridx =0;

                    __blockNSString  *trimString =@"";//截取出的字串

                    //使用字符串遍历,这个方法能准确知道每个emoji是占一个unicode还是两个

                    [text enumerateSubstringsInRange:NSMakeRange(0, [text length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock: ^(NSString* substring,NSRange substringRange,NSRange enclosingRange,BOOL* stop) {

                        if(idx >= rg.length) {

                            *stop =YES;//取出所需要就break,提高效率

                            return;

                        }

                        trimString = [trimString stringByAppendingString:substring];

                        idx++;

                    }];

                    s = trimString;

                }

                //rang是指从当前光标处进行替换处理(注意如果执行此句后面返回的是YES会触发didchange事件)

                textView.text = [textView.text stringByReplacingCharactersInRange:range withString:[text substringWithRange:rg]];

            }

            returnNO;

        }

    }

    #pragma mark -- UICollectionViewDataSource

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

        if([KLUtil isSmallScreen]) {

            if(_dataSource.count >=3) {

                return4;

            }else{

                return_dataSource.count +1;

            }

        }else{

            if(_dataSource.count >=4) {

                return5;

            }else{

                return_dataSource.count +1;

            }

        }

    }

    - (__kindofUICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

        TSessionTeamMemberCell *cellA = [collectionView dequeueReusableCellWithReuseIdentifier:T_SessionTeam_Member_Cell forIndexPath:indexPath];

        if(_dataSource.count >0&& indexPath.row < _dataSource.count) {

            NSDictionary *dic = _dataSource[indexPath.row];

            cellA.cellDic = dic;

        }

        returncellA;

    }

    #pragma mark -- UICollectionViewDelegate

    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

        TSessionTeamMemberCell *cell = (TSessionTeamMemberCell *)[collectionView cellForItemAtIndexPath:indexPath];

        if(!cell.cellDic) {

            DLog(@"点击了加号cell");

        }

    }

    #pragma mark -- UICollectionViewDelegateFlowLayout

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

        returnCGSizeMake(50,50);

    }

    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

        returnUIEdgeInsetsMake(0,0,0,0);

    }

    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

        CGFloat spacingF;

        if(kScreenWidth >320) {

            //4寸以上的手机

            spacingF = (kScreenWidth -50*5) /6.0;

        }else{

            //4寸手机

            spacingF = (kScreenWidth -50*4) /5.0;

        }

        returnspacingF;

    }

    - (void)didReceiveMemoryWarning {

        [superdidReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    /*

    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        // Get the new view controller using [segue destinationViewController].

        // Pass the selected object to the new view controller.

    }

    */

    @end

    相关文章

      网友评论

          本文标题:UIViewController里的控件自适应的时候,修改其他控

          本文链接:https://www.haomeiwen.com/subject/hxikyftx.html