设为首页 加入收藏

TOP

iOS 学习 - 11.圆角(小于等于四个)类似气泡和计算字符高度
2017-10-13 10:29:05 】 浏览:5244
Tags:iOS 学习 11. 圆角 小于 等于 类似 气泡 计算 字符 高度

使用贝塞尔曲线,

// 小于四个角 圆角
-(void)setbor{ NSString *str = @" couldn't fit this all in a comment to @lomanf's answer. So I'm adding it as an answer."; //计算字符高度
 [Corner layoutHeightWithLable:self.label text:str]; /* 1.使用空白 view addSubView label 2.得到类似 qq 聊天气泡,仅仅只是类似,还是有区别 */ UIView *view = [[UIView alloc]init]; //view 的 frame 要比 label 大一点,不然切圆角会切到字符串
    view.frame = CGRectMake(50 , 50 , _label.frame.size.width + 20 , _label.frame.size.height + 20); view.backgroundColor = [UIColor redColor]; //圆角
    [Corner createCornerInView:view corners:UIRectCornerBottomLeft|UIRectCornerTopRight cgsize:CGSizeMake(10, 10)]; [self.view addSubview:view]; [view addSubview:_label]; }

新建了一个类:Corner

.h

#import <UIKit/UIKit.h>

@interface Corner : UIView +(void)createCornerInView:(UIView *)view corners:(UIRectCorner)corner cgsize:(CGSize)size; +(void)layoutHeightWithLable:(UILabel *)label text:(NSString *)text; @end

.m

+(void)layoutHeightWithLable:(UILabel *)label text:(NSString *)text{ label.numberOfLines = 0; label.lineBreakMode = NSLineBreakByWordWrapping; CGSize maxSize = CGSizeMake(300, MAXFLOAT); CGSize textSize = [text boundingRectWithSize:maxSize options:(NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:label.font} context:nil].size; label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, textSize.width, textSize.height); label.text = text; } +(void)createCornerInView:(UIView *)view corners:(UIRectCorner)corner cgsize:(CGSize)size{ /* typedef NS_OPTIONS(NSUInteger, UIRectCorner) { UIRectCornerTopLeft = 1 << 0, - 左上角 UIRectCornerTopRight = 1 << 1, - 右上角 UIRectCornerBottomLeft = 1 << 2, - 左下角 UIRectCornerBottomRight = 1 << 3, - 右下角 UIRectCornerAllCorners = ~0UL - 四只角 }; */
    //贝塞尔曲线
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corner cornerRadii:size]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init]; maskLayer.frame = view.bounds; maskLayer.path = bezierPath.CGPath; view.layer.mask = maskLayer; }

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇加密 下一篇多线程简单总结

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目