• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

iOS控件使用和多样布局

武飞扬头像
ios_prestige
帮助1

一、得到字符串的宽度

  1.  
    - (NSArray *)dataArray {
  2.  
    if (!_dataArray) {
  3.  
    _dataArray = [NSArray array];
  4.  
    _dataArray = @[@"君不见我的天",@"黄河之水天上来",@"奔流到海不复回",@"君不见",@"高堂明镜悲白发",@"朝如青丝暮成雪",@"人生得意须尽欢",@"莫使金樽空对月",@"天生我材必有用",@"千金散尽还复来",@"烹羊宰牛且为乐",@"会须一饮三百杯",@"岑夫子",@"丹丘生",@"将进酒",@"君不见",@"黄河之水天上来",@"奔流到海不复回",@"君不见",@"高堂明镜悲白发",@"朝如青丝暮成雪",@"人生得意须尽欢",@"莫使金樽空对月",@"天生我材必有用",@"千金散尽还复来",@"烹羊宰牛且为乐",@"会须一饮三百杯",@"岑夫子",@"丹丘生",@"将进酒"];
  5.  
    }
  6.  
    return _dataArray;
  7.  
    }

定义一个数组,使用- (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));方法就可以得到这个字符串的宽度

  1.  
    for (int i = 0; i<30; i ) {
  2.  
    NSString *string = self.dataArray[i];
  3.  
    CGSize size = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, 30) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14.0]} context:nil].size;
  4.  
    NSLog(@"%f",size.width);
  5.  
    }

查看打印结果:

学新通

可以看到每个字符串的宽度

关于方法参数的介绍:

//参数一: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

案例演示:

  1.  
    UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 300, 0, 0)];
  2.  
    textLabel.backgroundColor = [UIColor grayColor];
  3.  
    textLabel.font = [UIFont systemFontOfSize:20];
  4.  
    textLabel.text = @"北京欢迎您";
  5.  
    [textLabel sizeToFit];
  6.  
    NSLog(@"width = %.1f height = %.1f",textLabel.frame.size.width,textLabel.frame.size.height);
  7.  
    [self.view addSubview:textLabel];

查看打印结果:Label的原始宽高进行了变化

学新通

  1.  
    UILabel *test1Label = [[UILabel alloc]initWithFrame:CGRectMake(20, 350, 0, 0)];
  2.  
    // UILabel *test1Label = [[UILabel alloc]init];
  3.  
    test1Label.backgroundColor = [UIColor redColor];
  4.  
    test1Label.font = [UIFont systemFontOfSize:20];
  5.  
    test1Label.text = @"北京欢迎您";
  6.  
    CGSize sizeThatFits = [test1Label sizeThatFits:CGSizeZero];
  7.  
    // test1Label.frame = CGRectMake(20, 350, sizeThatFits.width, sizeThatFits.height);
  8.  
    NSLog(@"sizeThatFits: width=%.1f height=%.1f", sizeThatFits.width, sizeThatFits.height);
  9.  
     
  10.  
    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控件

  1.  
    NSBundle *mainBundle = [NSBundle mainBundle];
  2.  
    NSLog(@"%@",mainBundle);
  3.  
    UIView *view = [[mainBundle loadNibNamed:@"CommentView" owner:nil options:nil] lastObject];
  4.  
    view.frame = CGRectMake(20, 100, 375, 40);
  5.  
    self.backView = view;
  6.  
    [self.view addSubview:view];
  7.  
     
  8.  
    UILabel *label = [view viewWithTag:10];
  9.  
    self.label = label;
  10.  
    NSLog(@"label的文字是%@",self.label.text);
  11.  
    UITextView *textView = [view viewWithTag:100];
  12.  
    self.textView = textView;
  13.  
    self.textView.delegate = self;

2)设置UITextViewDelegate代理,实现当文本改变时改变textView的高度

  1.  
    //输入时改变
  2.  
    - (void)textViewDidChange:(UITextView *)textView {
  3.  
    if ([textView.text isEqualToString:@""]) {
  4.  
    self.label.hidden = NO;
  5.  
    }else {
  6.  
    self.label.hidden = YES;
  7.  
    }
  8.  
    [self changeTextViewStyle:textView];
  9.  
    }
  1.  
    //改变TV样式
  2.  
    - (void)changeTextViewStyle :(UITextView *)TV {
  3.  
    CGSize size = [TV sizeThatFits:CGSizeMake(TV.frame.size.width, MAXFLOAT)];
  4.  
    NSLog(@"TV的高度是%f",size.height);
  5.  
    if (size.height<TV.frame.size.height) {
  6.  
    size.height = TV.frame.size.height;
  7.  
    }
  8.  
     
  9.  
    //现在最高高度
  10.  
    CGFloat maxHeight = TV.frame.size.height * 3;
  11.  
    size.height = MIN(size.height,maxHeight);
  12.  
     
  13.  
    CGRect TVRect = TV.frame;
  14.  
    TVRect.size.height = size.height;
  15.  
    TV.frame = TVRect;
  16.  
    // CGFloat TVSize = TV.frame.size.height;
  17.  
    // TVSize = size.height;
  18.  
    // TV.frame.size.height = TVSize;
  19.  
    // CGFloat viewHeight = self.backView.frame.size.height;
  20.  
    // viewHeight = size.height 10;
  21.  
    CGRect viewRect = self.backView.frame;
  22.  
    viewRect.size.height = size.height 10;
  23.  
    self.backView.frame = viewRect;
  24.  
    }
学新通

实现的结果:输入框的高度随文本的改变而改变

学新通

学新通

3)需要注意一下改变高度方法中 注释掉的部分,我们不能直接根据self.frame.size.height进行赋值,不然会报如下错误:表达式不能进行赋值,需要给一个中间变量

学新通

三、CAShapeLayer使用

1.CAShapeLayer继承自CALayer,需要和贝塞尔曲线配合使用才有意义

2.使用CAShapeLayer和贝塞尔曲线可以实现不在view的drawRect方法中画出一些想要的图形

3.CAShapeLayer和drawRect的区别:

drawRect:属于CoreGraphic框架,占用CPU,消耗性能大

CAShapeLayer:属于CoreAnimation框架,通过GPU来渲染图形,节省性能,动画渲染直接提交给手机GPU,不消耗内存

  1.  
    - (void)viewDidLoad {
  2.  
    [super viewDidLoad];
  3.  
     
  4.  
    //创建path
  5.  
    UIBezierPath *bezi = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)];
  6.  
     
  7.  
    CAShapeLayer *layer = [[CAShapeLayer alloc]init];
  8.  
    layer.path = bezi.CGPath;
  9.  
    //路径的颜色
  10.  
    layer.strokeColor = [UIColor redColor].CGColor;
  11.  
     
  12.  
    //填充色
  13.  
    layer.fillColor = [UIColor greenColor].CGColor;
  14.  
     
  15.  
    //线宽
  16.  
    layer.lineWidth = 15;
  17.  
     
  18.  
    //默认不设置是实线绘制,如果设置那么会虚线绘制,绘制前一个值是绘制虚线粗细,后一个值是间隔粗细
  19.  
    layer.lineDashPattern = @[@20,@2];
  20.  
    // baseView.layer.mask = layer;
  21.  
    [self.view.layer addSublayer:layer];
  22.  
     
  23.  
    }
学新通

实现的效果:

学新通

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个字颜色不一样,为红色。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcbjfe
系列文章
更多 icon
同类精品
更多 icon
继续加载