美文网首页
在label上添加btn

在label上添加btn

作者: summerTa | 来源:发表于2018-08-14 17:56 被阅读0次

    #import "ViewController.h"

    #import

    #import

    #import

    #import

    @interface ViewController ()

    @property(nonatomic,strong)YYLabel *label;

    @property(nonatomic,strong)UILabel *textLabel;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        NSMutableAttributedString *text = [NSMutableAttributedString new];

        UIFont*font = [UIFontsystemFontOfSize:16];

        NSString *title = @"dwwdqwddqdqdqdqwdqdqwdqwdqdqdqdqwdqwdqdqdqwdqdqwdqdqdqdqdqdqwdq当前dwwdqwddqdqdqdqwdqdqwdqwdqdqdqdqwdqwdqdqdqwdqdqwdqdqdqdqdqdqwdq当前dwwdqwddqdqdqdqwdqdqwdqwdqdqdqdqwdqwdqdqdqwdqdqwdqdqdqdqdqdqwdq当前dwwdqwddqdqdqdqwdqdqwdqwdqdqdqdqwdqwdqdqdqwdqdqwdqdqdqdqdqdqwdq当前dwwdqwddqdqdqdqwdqdqwdqwdqdqdqdqwdqwdqdqdqwdqdqwdqdqdqdqdqdqwdq当前";

        [textappendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]];

        text.yy_font= font ;

        _label= [YYLabelnew];

        _label.userInteractionEnabled = YES;

        _label.numberOfLines = 0;

        _label.textVerticalAlignment = YYTextVerticalAlignmentTop;

        _label.frame = CGRectMake(40,60, self.view.frame.size.width-80,60);

        _label.attributedText = text;

        [self.view addSubview:_label];

        _label.layer.borderWidth = 0.5;

        _label.layer.borderColor = [UIColor colorWithRed:0.000 green:0.463 blue:1.000 alpha:1.000].CGColor;

        // 添加全文

        [self addSeeMoreButton];

        _textLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 200, 100)];

        _textLabel.text = @"1234567";

        _textLabel.textAlignment = NSTextAlignmentLeft;

        _textLabel.backgroundColor = [UIColor redColor];

        [self.view addSubview:_textLabel];

        UIButton*btn = [[UIButtonalloc]init];

        btn.backgroundColor = [UIColor blueColor];

        [btnaddTarget:self action:@selector(clickbtn:) forControlEvents:UIControlEventTouchUpInside];

        [_textLabeladdSubview:btn];

        _textLabel.userInteractionEnabled = YES;

        [_textLabel bringSubviewToFront:btn];

        [btnmas_makeConstraints:^(MASConstraintMaker*make) {

            make.right.mas_equalTo(0);

            make.bottom.mas_equalTo(0);

            make.size.mas_equalTo(CGSizeMake(60,30));

        }];

    }

    -(void)clickbtn:(UIButton*)btn{

        NSLog(@"点击的是按钮");

    }

    #pragma mark - 添加全文

    - (void)addSeeMoreButton {

        __weak __typeof(self) weakSelf = self;

        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"...全文"];

        YYTextHighlight *hi = [YYTextHighlight new];

        [hisetColor:[UIColor colorWithRed:0.578 green:0.790 blue:1.000 alpha:1.000]];

        hi.tapAction= ^(UIView*containerView,NSAttributedString*text,NSRangerange,CGRectrect) {

            // 点击全文回调

    //        YYLabel *label = weakSelf.label;

    //        [label sizeToFit];

            NSLog(@"点击yytext问题");

        };

        [textyy_setColor:[UIColor colorWithRed:0.000 green:0.449 blue:1.000 alpha:1.000] range:[text.string rangeOfString:@"全文"]];

        [textyy_setTextHighlight:hi range:[text.string rangeOfString:@"全文"]];

        text.yy_font=_label.font;

        YYLabel*seeMore = [YYLabelnew];

        seeMore.attributedText= text;

        [seeMoresizeToFit];

        NSAttributedString *truncationToken = [NSAttributedString yy_attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize:seeMore.frame.size alignToFont:text.yy_font alignment:YYTextVerticalAlignmentCenter];

        NSLog(@"输出_label.length:%f",_label.frame.size.height);

        NSInteger a = _label.frame.size.height;

        if(a<40) {

            NSLog(@"高度小于40");

        }else{

            NSLog(@"高度大于40");

             _label.truncationToken= truncationToken;

        }

    }

    @end

    相关文章

      网友评论

          本文标题:在label上添加btn

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