iOS计算字符串的宽度高度
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计算字符串的宽度高度的更多相关文章
- iOS 计算字符串显示宽高度
		ObjC(Category of NSString): - (CGSize)getSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size{ ... 
- iOS中计算字符串NSString的高度
		根据固定宽度计算字符串高度: NSString *info = @"但是公司的高度是广东省公司的广东省高速度来开个大帅哥多撒谎个爱好就跟他说噶三公司噶是的刚好是我哥如果黑暗如果坏都干撒降低公 ... 
- 【代码笔记】iOS-获取字符串的宽度,高度
		一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, ... 
- ios 计算字符串长度<转>
		- (int)textLength:(NSString *)text//计算字符串长度 { float number = 0.0; for (int index = 0; index ... 
- iOS计算富文本(NSMutableAttributedString)高度
		有时候开发中我们为了样式好看, 需要对文本设置富文本属性, 设置完后那么怎样计算其高度呢, 很简单, 方法如下: - (NSInteger)hideLabelLayoutHeight:(NSStrin ... 
- ios计算字符串宽高,指定字符串变色,获取URL参数集合
		#import <Foundation/Foundation.h> @interface NSString (Extension) - (CGFloat)heightWithLimitWi ... 
- iOS: 计算 UIWebView 的内容高度
		- (void)webViewDidFinishLoad:(UIWebView *)wb { //方法1 CGFloat documentWidth = [[wb stringByEvaluating ... 
- iOS依据字符串计算UITextView高度
		iOS计算字符串高度,有须要的朋友能够參考下. 方法一:ios7.0之前适用 /** @method 获取指定宽度width,字体大小fontSize,字符串value的高度 @param value ... 
- 关于UIFont和计算字符串的高度和宽度
		转自:http://i.cnblogs.com/EditPosts.aspx?opt=1 1.创建方法:+ fontWithName:size:- fontWithSize:2.创建系统字体:+ sy ... 
随机推荐
- 怎样更改wordpress登陆 URL防止恶意注册
			WP 默认的登陆 URL 是 wp-login.php或wp-admin.php,许多spamer会根据这些footprint来收集可注册的wordpress站点,然后你的站内就多出许多垃圾评论.如果 ... 
- 设置p标签自动换行
			<body> <p style="width:20px;height:100px;background-color:#069; word-wrap: break-w ... 
- Apache常用2种工作模式prefork和worker比较
			Apache两种常用工作模式:prefork和worker. prefork MPM prefork是一个非线程型的.预派生的MPM,使用多个进程,每个进程在某个确定的时间只单独处理一个连接,效率高, ... 
- ubutu之Navicat安装
			1.下载navicat111_premium_cs.tar.gz压缩包 2.进入压缩包所在目录,执行一下命令解压 tar -zxvf navicat111_premium_cs.r.gz 3.进入解压 ... 
- DateEdit和TimeEdit用法
			DateEdit 控件默认情况下,显示的只有日期,没有时间.下面介绍2中日期和时间同时显示的方法: 1.Properties.VistaDisplayMode 为true, 2.Properties. ... 
- 用jQuery编的一个分页小代码
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ... 
- oracle with as 用法
			http://blog.itpub.net/28371090/viewspace-1190141/ 
- 问题: Oracle Database 10g 未在当前操作系统中经过认证
			问题: Oracle Database 10g 未在当前操作系统中经过认证 在Windows 7中安装Oracle 10g. 使用的Orcale版本是10g. 步骤1: 在Orcale官网上下载,下载 ... 
- 2-python学习——hello world
			"hello world"是编程界一个经久不衰的例子,几乎所有语言的学习教程都把它当做第一个程序的范例.学习的过程就是再造轮子的过程,千万不要以为有人做过的,就不去学习了. hel ... 
- Python自动化之面向对象进阶
			1 静态方法 静态方法是不可以访问实例变量或类变量的,一个不能访问实例变量和类变量的方法,其实相当于跟类本身已经没什么关系了,它与类唯一的关联就是需要通过类名来调用这个方法. class Dog(ob ... 
