OC开发中会遇到根据字符串和字体大小来算计算出字符串所占的宽高->> 封装方法如下:

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@interface XSDKResourceUtil : NSObject

//获取字符串宽

+(CGSize)measureSinglelineStringSize:(NSString*)str andFont:(UIFont*)wordFont;

//获取字符串宽 // 传一个字符串和字体大小来返回一个字符串所占的宽度

+(float)measureSinglelineStringWidth:(NSString*)str andFont:(UIFont*)wordFont;

//获取字符串高 // 传一个字符串和字体大小来返回一个字符串所占的高度

+(float)measureMutilineStringHeight:(NSString*)str andFont:(UIFont*)wordFont andWidthSetup:(float)width;

+(UIImage*)imageAt:(NSString*)imgNamePath;

+(BOOL)xsdkcheckName:(NSString*)name;

+(BOOL)xsdkcheckPhone:(NSString *)userphone;

+ (UIColor *)xsdkcolorWithHexString:(NSString *)color alpha:(CGFloat)alpha;

+(BOOL)xsdkstringIsnilOrEmpty:(NSString*)string;

+(BOOL)jsonFieldIsNull:(id)jsonField;

+(int)filterIntValue:(id)value withDefaultValue:(int)defaultValue;

+(NSString*)filterStringValue:(id)value withDefaultValue:(NSString*)defaultValue;

@end

// -------------------------------------方法具体实现-------------------------------------------

#import "XSDKResourceUtil.h"

@implementation XSDKResourceUtil

+(float)measureMutilineStringHeight:(NSString*)str andFont:(UIFont*)wordFont andWidthSetup:(float)width{

if (str == nil || width <= 0) return 0;

CGSize measureSize;

if([[UIDevice currentDevice].systemVersion floatValue] < 7.0){

measureSize = [str sizeWithFont:wordFont constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

}else{

measureSize = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:wordFont, NSFontAttributeName, nil] context:nil].size;

}

return ceil(measureSize.height);

}

// 传一个字符串和字体大小来返回一个字符串所占的宽度

+(float)measureSinglelineStringWidth:(NSString*)str andFont:(UIFont*)wordFont{

if (str == nil) return 0;

CGSize measureSize;

if([[UIDevice currentDevice].systemVersion floatValue] < 7.0){

measureSize = [str sizeWithFont:wordFont constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

}else{

measureSize = [str boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:wordFont, NSFontAttributeName, nil] context:nil].size;

}

return ceil(measureSize.width);

}

+(CGSize)measureSinglelineStringSize:(NSString*)str andFont:(UIFont*)wordFont

{

if (str == nil) return CGSizeZero;

CGSize measureSize;

if([[UIDevice currentDevice].systemVersion floatValue] < 7.0){

measureSize = [str sizeWithFont:wordFont constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

}else{

measureSize = [str boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:wordFont, NSFontAttributeName, nil] context:nil].size;

}

return measureSize;

}

//+(UIImage*)imageAt:(NSString*)imgNamePath{

//    if (imgNamePath == nil || [[imgNamePath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]length] == 0) {

//        return nil;

//    }

//    return [UIImage imageNamed:[ImageResourceBundleName stringByAppendingPathComponent:imgNamePath]];

//}

+(BOOL)xsdkcheckName:(NSString*)name{

if([XSDKResourceUtil xsdkstringIsnilOrEmpty:name]){

return NO;

}else{

if(name.length < 5){

return NO;

}

if(name.length > 20){

return NO;

}

NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^[a-zA-Z][a-zA-Z0-9_]*$"];

if(![pred evaluateWithObject:name]){

return [XSDKResourceUtil xsdkcheckPhone:name];

}

}

return YES;

}

+(BOOL)xsdkcheckPhone:(NSString *)userphone

{

NSPredicate * phone = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^1\\d{10}"];

if (![phone evaluateWithObject:userphone]) {

return NO;

}

return YES;

}

+(BOOL)xsdkstringIsnilOrEmpty:(NSString*)string{

if (string == nil || [string isKindOfClass:[NSNull class]]  || [string isEqualToString:@""]) {

return YES;

}else{

return NO;

}

}

+(UIColor *)xsdkcolorWithHexString:(NSString *)color alpha:(CGFloat)alpha

{

//删除字符串中的空格

NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

// String should be 6 or 8 characters

if ([cString length] < 6)

{

return [UIColor clearColor];

}

// strip 0X if it appears

//如果是0x开头的,那么截取字符串,字符串从索引为2的位置开始,一直到末尾

if ([cString hasPrefix:@"0X"])

{

cString = [cString substringFromIndex:2];

}

//如果是#开头的,那么截取字符串,字符串从索引为1的位置开始,一直到末尾

if ([cString hasPrefix:@"#"])

{

cString = [cString substringFromIndex:1];

}

if ([cString length] != 6)

{

return [UIColor clearColor];

}

// Separate into r, g, b substrings

NSRange range;

range.location = 0;

range.length = 2;

//r

NSString *rString = [cString substringWithRange:range];

//g

range.location = 2;

NSString *gString = [cString substringWithRange:range];

//b

range.location = 4;

NSString *bString = [cString substringWithRange:range];

// Scan values

unsigned int r, g, b;

[[NSScanner scannerWithString:rString] scanHexInt:&r];

[[NSScanner scannerWithString:gString] scanHexInt:&g];

[[NSScanner scannerWithString:bString] scanHexInt:&b];

return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];

}

+(BOOL)jsonFieldIsNull:(id)jsonField{

return (jsonField == nil || [jsonField isKindOfClass:[NSNull class]]);

}

+(int)filterIntValue:(id)value withDefaultValue:(int)defaultValue{

if (![XSDKResourceUtil jsonFieldIsNull:value]) {

return [value intValue];

}else{

return defaultValue;

}

}

+(NSString*)filterStringValue:(id)value withDefaultValue:(NSString*)defaultValue{

if ([value isKindOfClass:[NSString class]] && ![XSDKResourceUtil xsdkstringIsnilOrEmpty:value]) {

return value;

}else{

return defaultValue;

}

}

@end

iOS计算字符串的宽度高度的更多相关文章

  1. iOS 计算字符串显示宽高度

    ObjC(Category of NSString): - (CGSize)getSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size{ ...

  2. iOS中计算字符串NSString的高度

    根据固定宽度计算字符串高度: NSString *info = @"但是公司的高度是广东省公司的广东省高速度来开个大帅哥多撒谎个爱好就跟他说噶三公司噶是的刚好是我哥如果黑暗如果坏都干撒降低公 ...

  3. 【代码笔记】iOS-获取字符串的宽度,高度

    一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, ...

  4. ios 计算字符串长度<转>

    - (int)textLength:(NSString *)text//计算字符串长度 {     float number = 0.0;     for (int index = 0; index ...

  5. iOS计算富文本(NSMutableAttributedString)高度

    有时候开发中我们为了样式好看, 需要对文本设置富文本属性, 设置完后那么怎样计算其高度呢, 很简单, 方法如下: - (NSInteger)hideLabelLayoutHeight:(NSStrin ...

  6. ios计算字符串宽高,指定字符串变色,获取URL参数集合

    #import <Foundation/Foundation.h> @interface NSString (Extension) - (CGFloat)heightWithLimitWi ...

  7. iOS: 计算 UIWebView 的内容高度

    - (void)webViewDidFinishLoad:(UIWebView *)wb { //方法1 CGFloat documentWidth = [[wb stringByEvaluating ...

  8. iOS依据字符串计算UITextView高度

    iOS计算字符串高度,有须要的朋友能够參考下. 方法一:ios7.0之前适用 /** @method 获取指定宽度width,字体大小fontSize,字符串value的高度 @param value ...

  9. 关于UIFont和计算字符串的高度和宽度

    转自:http://i.cnblogs.com/EditPosts.aspx?opt=1 1.创建方法:+ fontWithName:size:- fontWithSize:2.创建系统字体:+ sy ...

随机推荐

  1. CentOS6.3编译安装Memcached集群分布式缓存代理Magent-0.6出错汇总

    参考文章:Memcached集群/分布式/高可用 及 Magent缓存代理搭建过程 详解,搭建Magent,在编译的过程中会出现很多错误: #编译安装安装magent到 /usr/local/mage ...

  2. Java项目相关监控与调优

    Linux  JVM Tomcat =========Linux =============== 监控 nmon  命令:nmon -s 10 -c 60 -f -m /home -s 10 每10s ...

  3. 常见HTTP错误代码大全

    一些常见的状态码为: 200 - 服务器成功返回网页404 - 请求的网页不存在503 - 服务不可用详细分解: 1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明100 ...

  4. BZOJ1367——[Baltic2004]sequence

    1.题目大意:给一个序列t,然后求一个序列z,使得$|z1-t1|+|z2-t2|+...+|zn-tn|$的值最小,我们只需要求出这个值就可以了,并且z序列是递增的 2.分析:这道题z序列是递增的, ...

  5. 最牛X的GCC 内联汇编

    导读 正如大家知道的,在C语言中插入汇编语言,其是Linux中使用的基本汇编程序语法.本文将讲解 GCC 提供的内联汇编特性的用途和用法.对于阅读这篇文章,这里只有两个前提要求,很明显,就是 x86 ...

  6. Android应用签名

    http://www.cnblogs.com/ghj1976/archive/2011/07/18/2109381.html 为了要签名? 开发Android的人这么多,完全有可能大家都把类名,包名起 ...

  7. All Kind Of Conference(随时更新...)

    收集一些前端开发的各种会议,里面有视频或者PPT,随时查看都还是很有收获的.还有要向这些演讲的前辈看齐- AC 2015:http://ac.alloyteam.com/2015/ AC 2016:h ...

  8. ion-slide-box,无限循环

    ion-slide-box网络加载图片,及时更新,无限循环 does-continue:是否循环切换,开头的幻灯页只能向左滑动,最后的幻灯页只能向右滑动. 将does-continue属性值设为tru ...

  9. svn update错误

    可以再checkout下来一份项目,重新命名,然后将该项目下的隐藏文件夹.svn替换掉原项目 注意备份

  10. 保存字符串到手机SDcard为txt文件

    try { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File sdCardDir ...