zoukankan      html  css  js  c++  java
  • iOS 根据字符串数目,自定义Label等控件的高度

    利用分类,NSString,增加一个方法。

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    @interface NSString (Height)
    
    + (CGSize)getRect:(NSString *)textStr andWidth:(CGSize)size andFont:(UIFont *)font;
    
    @end
    #import "NSString+Height.h"
    
    @implementation NSString (Height)
    
    + (CGSize)getRect:(NSString *)textStr andWidth:(CGSize)size andFont:(UIFont *)font
    {
        
        CGRect rect = [textStr boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
        
        return rect.size;
        
    }
    
    @end

    使用:

    CGSize size = [NSString getRect:str andWidth:CGSizeMake(IOS_SCREEN.size.width/2 - 10, CGFLOAT_MAX) andFont:[UIFont systemFontOfSize:14]];
    其中:
    str 是将要计算的字符串,
    IOS_SCREEN.size.width/2 - 10 是宽度,
    [UIFont systemFontOfSize:14]是设置字号
    这些需要自己根据情况写;
  • 相关阅读:
    零散的学习总结
    JSON学习整理
    轮播图
    关于new Object的小结
    js函数声明和函数表达式的区别
    float小结
    DOM文档加载步骤
    css主要的浏览器兼容性问题
    js for循环小记
    CANVAS中的lineWidth小计
  • 原文地址:https://www.cnblogs.com/jukaiit/p/5161474.html
Copyright © 2011-2022 走看看