一、得到字符串的宽度
- (NSArray *)dataArray {
if (!_dataArray) {
_dataArray = [NSArray array];
_dataArray = @[@"君不见我的天",@"黄河之水天上来",@"奔流到海不复回",@"君不见",@"高堂明镜悲白发",@"朝如青丝暮成雪",@"人生得意须尽欢",@"莫使金樽空对月",@"天生我材必有用",@"千金散尽还复来",@"烹羊宰牛且为乐",@"会须一饮三百杯",@"岑夫子",@"丹丘生",@"将进酒",@"君不见",@"黄河之水天上来",@"奔流到海不复回",@"君不见",@"高堂明镜悲白发",@"朝如青丝暮成雪",@"人生得意须尽欢",@"莫使金樽空对月",@"天生我材必有用",@"千金散尽还复来",@"烹羊宰牛且为乐",@"会须一饮三百杯",@"岑夫子",@"丹丘生",@"将进酒"];
}
return _dataArray;
}
定义一个数组,使用- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attributes context:(nullable NSStringDrawingContext *)context API_AVAILABLE(macos(10.11), ios(7.0));方法就可以得到这个字符串的宽度
for (int i = 0; i<30; i++) {
NSString *string = self.dataArray[i];
CGSize size = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, 30) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14.0]} context:nil].size;
NSLog(@"%f",size.width);
}
查看打印结果:
可以看到每个字符串的宽度
关于方法参数的介绍:
//参数一:size 表示计算文本的最大宽高
//参数二:表示计算的类型。
NSStringDrawingUsesLineFragmentOrigin绘制文本时使用 line fragement origin 而不是 baseline origin。一般使用这项
NSStringDrawingUsesFontLeading 根据字体计算高度
NSStringDrawingUsesDeviceMetrics 使用象形文字计算高度NSStringDrawingTruncatesLastVisibleLine 如果NSStringDrawingUsesLineFragmentOrigin设置,这个选项中没有用
//参数三:attributes 表示富文本的属性
二、实现输入框自适应高度
1.在实现自适应宽度之前,首先介绍两个方法
1)- (void)sizeToFit;
此方法会计算出最优的size并且改变自己的size
2)- (CGSize)sizeThatFits:(CGSize)size;
此方法会计算出最优的size但是不会改变自己的size
案例演示:
UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 300, 0, 0)];
textLabel.backgroundColor = [UIColor grayColor];
textLabel.font = [UIFont systemFontOfSize:20];
textLabel.text = @"北京欢迎您";
[textLabel sizeToFit];
NSLog(@"width = %.1f height = %.1f",textLabel.frame.size.width,textLabel.frame.size.height);
[self.view addSubview:textLabel];
查看打印结果:Label的原始宽高进行了变化
UILabel *test1Label = [[UILabel alloc]initWithFrame:CGRectMake(20, 350, 0, 0)];
// UILabel *test1Label = [[UILabel alloc]init];
test1Label.backgroundColor = [UIColor redColor];
test1Label.font = [UIFont systemFontOfSize:20];
test1Label.text = @"北京欢迎您";
CGSize sizeThatFits = [test1Label sizeThatFits:CGSizeZero];
// test1Label.frame = CGRectMake(20, 350, sizeThatFits.width, sizeThatFits.height);
NSLog(@"sizeThatFits: width=%.1f height=%.1f", sizeThatFits.width, sizeThatFits.height);
NSLog(@"width=%.1f height=%.1f", test1Label.frame.size.width, test1Label.frame.size.height);
查看打印结果:sizeThatFits的值进行了变化,但是本身大小的值并没有改变
所以实现的效果,如果使用sizeThatFits方法,后面必须对frame进行重新赋值,不然宽高依旧是0,0(这里我设置的0,0),视图不显示
2.实现输入框自适应高度
便于演示,创建了一个xib,布局如下,包含一个textView,TextView里面加入了一个label。
1)加载xib,通过tag值找到textView和label控件
NSBundle *mainBundle = [NSBundle mainBundle];
NSLog(@"%@",mainBundle);
UIView *view = [[mainBundle loadNibNamed:@"CommentView" owner:nil options:nil] lastObject];
view.frame = CGRectMake(20, 100, 375, 40);
self.backView = view;
[self.view addSubview:view];
UILabel *label = [view viewWithTag:10];
self.label = label;
NSLog(@"label的文字是%@",self.label.text);
UITextView *textView = [view viewWithTag:100];
self.textView = textView;
self.textView.delegate = self;
2)设置UITextViewDelegate代理,实现当文本改变时改变textView的高度
//输入时改变
- (void)textViewDidChange:(UITextView *)textView {
if ([textView.text isEqualToString:@""]) {
self.label.hidden = NO;
}else {
self.label.hidden = YES;
}
[self changeTextViewStyle:textView];
}
//改变TV样式
- (void)changeTextViewStyle :(UITextView *)TV {
CGSize size = [TV sizeThatFits:CGSizeMake(TV.frame.size.width, MAXFLOAT)];
NSLog(@"TV的高度是%f",size.height);
if (size.height<TV.frame.size.height) {
size.height = TV.frame.size.height;
}
//现在最高高度
CGFloat maxHeight = TV.frame.size.height * 3;
size.height = MIN(size.height,maxHeight);
CGRect TVRect = TV.frame;
TVRect.size.height = size.height;
TV.frame = TVRect;
// CGFloat TVSize = TV.frame.size.height;
// TVSize = size.height;
// TV.frame.size.height = TVSize;
// CGFloat viewHeight = self.backView.frame.size.height;
// viewHeight = size.height +10;
CGRect viewRect = self.backView.frame;
viewRect.size.height = size.height + 10;
self.backView.frame = viewRect;
}
实现的结果:输入框的高度随文本的改变而改变
3)需要注意一下改变高度方法中 注释掉的部分,我们不能直接根据self.frame.size.height进行赋值,不然会报如下错误:表达式不能进行赋值,需要给一个中间变量
三、CAShapeLayer使用
1.CAShapeLayer继承自CALayer,需要和贝塞尔曲线配合使用才有意义
2.使用CAShapeLayer和贝塞尔曲线可以实现不在view的drawRect方法中画出一些想要的图形
3.CAShapeLayer和drawRect的区别:
drawRect:属于CoreGraphic框架,占用CPU,消耗性能大
CAShapeLayer:属于CoreAnimation框架,通过GPU来渲染图形,节省性能,动画渲染直接提交给手机GPU,不消耗内存
- (void)viewDidLoad {
[super viewDidLoad];
//创建path
UIBezierPath *bezi = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)];
CAShapeLayer *layer = [[CAShapeLayer alloc]init];
layer.path = bezi.CGPath;
//路径的颜色
layer.strokeColor = [UIColor redColor].CGColor;
//填充色
layer.fillColor = [UIColor greenColor].CGColor;
//线宽
layer.lineWidth = 15;
//默认不设置是实线绘制,如果设置那么会虚线绘制,绘制前一个值是绘制虚线粗细,后一个值是间隔粗细
layer.lineDashPattern = @[@20,@2];
// baseView.layer.mask = layer;
[self.view.layer addSublayer:layer];
}
实现的效果:
1.直接给Label设置文字
实现的效果如下:
2.设置文字样式
UILabel *jieshaoLabel = [[UILabel alloc]init];
jieshaoLabel.numberOfLines = 0;
NSString *jieshaoStr = @"植物花园是一款针对喜爱养花的人设计的app,用户可以添加鲜花,可以对花定期的进行浇水,可以看到自己的养护记录,清楚,简便";
NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithString:jieshaoStr];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
//其余行缩进
style.headIndent = 0;
//第一行缩进
style.firstLineHeadIndent = 22;
//行距
style.lineSpacing = 10;
[text addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, text.length)];
jieshaoLabel.attributedText = text;
jieshaoLabel.font = [UIFont systemFontOfSize:14];
jieshaoLabel.textColor = [UIColor grayColor];
[backView addSubview:jieshaoLabel];
[jieshaoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(20);
make.right.mas_equalTo(-20);
make.top.mas_equalTo(nameLabel.mas_bottom).offset(20);
}];
实现的效果:
3.使用NSMutableAttributedString对文本进行设置
NSString *s = [NSString stringWithFormat:@"目前已错%ld个字",(long)self.errorNum];
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:s];
//文字的整体字号
[attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, 7)];
//前4个文字的颜色
[attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, 4)];
//第5个文字的颜色
[attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4, 1)];
//第6-7个文字的颜色
[attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(5, 2)];
UILabel *label_b = [[UILabel alloc]init];
label_b.frame = CGRectMake(20, 20, 100, 40);
label_b.numberOfLines = 0;
label_b.attributedText = attributeString;
[self addSubview:label_b];
实现的效果如下:
这样就可以实现中间的第5个字颜色不一样,为红色。