仿安卓小小提示框

来源:互联网 发布:淘宝正品耐克店铺 编辑:程序博客网 时间:2024/04/29 13:50


-(void)showString:(NSString *)string


{

    UIWindow *window=[UIApplicationsharedApplication].keyWindow;

    

    UIView *bgView=[[UIViewalloc]initWithFrame:CGRectMake(1,1, 1,1)];

    bgView.backgroundColor=[UIColor blackColor];

    bgView.layer.cornerRadius=5.0f;

    bgView.layer.masksToBounds=YES;

    [window addSubview:bgView];

    CGSize lablesize=[stringsizeWithFont:[UIFontsystemFontOfSize: 15] constrainedToSize:CGSizeMake(self.view.frame.size.width - 40, 9000)];

    UILabel *lable=[[UILabelalloc]initWithFrame:CGRectMake(10,5, lablesize.width, lablesize.height)];

    lable.textAlignment=NSTextAlignmentLeft;

    lable.text=string;

    lable.numberOfLines=0;

    lable.textColor=[UIColorwhiteColor];

    lable.font=[UIFontsystemFontOfSize:15];

    [bgView addSubview:lable];

    

    bgView.frame=CGRectMake( (self.view.frame.size.width - lablesize.width -20)/2  , self.view.frame.size.height - 100, lablesize.width +20, lablesize.height +10);


    [UIViewanimateWithDuration:10animations:^{

        bgView.alpha=0;

    } completion:^(BOOL finished) {

        [bgView removeFromSuperview];

    }];

}


     讨论一下      Lable  的 自适应高度的方法

2.    lable    方法

   UILabel *textLabel = [[UILabelalloc] init];

      textLabel.font = [UIFontsystemFontOfSize:16];

        NSString *str =@"2222222222222222222222222222222222222sdssddddddddd22222";

      textLabel.text = str;

     textLabel.backgroundColor = [UIColorredColor];

     textLabel.numberOfLines =0;//根据最大行数需求来设置

     //textLabel.lineBreakMode = NSLineBreakByTruncatingTail;

       CGSize maximumLabelSize =CGSizeMake(100,9999);//labelsize的最大值

       //关键语句

     CGSize expectSize = [textLabelsizeThatFits:maximumLabelSize];

        //别忘了把frame给回label,如果用xib加了约束的话可以只改一个约束的值

    textLabel.frame =CGRectMake(20,70, expectSize.width, expectSize.height);

        [self.viewaddSubview:textLabel];

3.  



  UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(0,0, 100,46)];

    NSString *str=@"发恶无法奶奶问服务就啃发觉我看能否尽快稳罚款金额为你放假";

    label.text=str;

    UIFont *font=[UIFontsystemFontOfSize:15];

    label.font=font;

    

    //拆行

    //label.lineBreakMode=NSLineBreakByWordWrapping;

    //设置多行

    [label setNumberOfLines:0];

    

    //设置最大尺寸的宽和高

    CGSize size=CGSizeMake(100,1000);

    //获取当前字体

    NSDictionary *dict=@{NSFontAttributeName: [UIFontsystemFontOfSize:15]};

    CGSize authSize=[strboundingRectWithSize:size

                                      options:NSStringDrawingUsesLineFragmentOrigin

                                   attributes:dict

                                      context:nil].size;

    label.frame=CGRectMake(20,70, authSize.width, authSize.height);

    [self.viewaddSubview:label];













0 0
原创粉丝点击