iOS UILabel commonly used treatment

        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.font = [UIFont systemFontOfSize:16];
        NSString *str = @"2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222";
        textLabel.text = str;
        textLabel.backgroundColor = [UIColor redColor];
        textLabel.numberOfLines = 0;//根据最大行数需求来设置(0就是不做行数限制)
        textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        CGSize maximumLabelSize = CGSizeMake(100, 9999);//labelsize的最大值
        //计算高度
        CGSize expectSize = [textLabel sizeThatFits:maximumLabelSize];
        //别忘了把frame给回label,如果用xib加了约束的话可以只改一个约束的值
        textLabel.frame = CGRectMake(20, 70, expectSize.width, expectSize.height);
        [self.view addSubview:textLabel];
// view切圆角
textLabel.layer.cornerRadius = 39;
textLabel.layer.masksToBounds = YES;
//设置边框及边框颜色
textLabel.layer.borderWidth = 8;
textLabel.layer.borderColor =[ [UIColor grayColor] CGColor];

//切圆角
//设置所需的圆角位置以及大小
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:aView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = aView.bounds;
    maskLayer.path = maskPath.CGPath;
    aView.layer.mask = maskLayer;
 });
/*
代码量偏多,且很多 UIView 都是使用约束布局,必须搭配 dispatch_after 函数来设置自身的 mask。因为只有在此时,才能把 UIView 的正确的 bounds 设置到 CAShapeLayer 的 frame 上。
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
    UIRectCornerTopLeft     = 1 << 0,
    UIRectCornerTopRight    = 1 << 1,
    UIRectCornerBottomLeft  = 1 << 2,
    UIRectCornerBottomRight = 1 << 3,
    UIRectCornerAllCorners  = ~0UL
};
*/
//设置边框线
//设置阴影
containerView.backgroundColor = [UIColor clearColor];
containerView.layer.shadowOffset = CGSizeMake(0, 2);
containerView.layer.shadowOpacity = 0.80;

END

Guess you like

Origin blog.csdn.net/weixin_34198762/article/details/90861716