zoukankan      html  css  js  c++  java
  • iOS 字符串的宽度和高度自适应

    //获取字符串的宽度
    -(float)widthForString:(NSString *)value fontSize:(float)fontSize andHeight:(float)height
    {
        UIColor  *backgroundColor=[UIColor blackColor];
        UIFont *font=[UIFont boldSystemFontOfSize:fontSize];
        CGRect sizeToFit = [value boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{
                                                                                                                                                 NSForegroundColorAttributeName:backgroundColor,
                                                                                                                                                 NSFontAttributeName:font
                                                                                                                                                 } context:nil];
        
        return sizeToFit.size.width;
    }
    //获得字符串的高度
    -(float) heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width
    {
        UIColor  *backgroundColor=[UIColor blackColor];
        UIFont *font=[UIFont boldSystemFontOfSize:18.0];
        CGRect sizeToFit = [value boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{
                                           NSForegroundColorAttributeName:backgroundColor,
                                           NSFontAttributeName:font
                                           } context:nil];
        return sizeToFit.size.height;
    }
  • 相关阅读:
    flex布局
    input框不能输入问题
    JS的innerHTML完成注册表
    CSS的z-index属性和box-shadow属性
    JS个人笔记
    css照片墙
    透明度设置
    a标签的name属性
    iframe标签
    title属性
  • 原文地址:https://www.cnblogs.com/ceasar/p/6223473.html
Copyright © 2011-2022 走看看