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 ...
随机推荐
- 【python网络编程】新浪爬虫:关键词搜索爬取微博数据
上学期参加了一个大数据比赛,需要抓取大量数据,于是我从新浪微博下手,本来准备使用新浪的API的,无奈新浪并没有开放关键字搜索的API,所以只能用爬虫来获取了.幸运的是,新浪提供了一个高级搜索功能,为我 ...
- servlet和http请求
1.servlet servlet是和平台无关的服务器组件,可以交互式的来浏览和修改数据,生成动态的web内容.它运行于 servlet容器中2.servlet容器 servlet容器负责servle ...
- CentOS7安装elk
192.168.161.128 elk.test.com jdk-8u102-linux-x64.rpm elasticsearch-2.3.3.rpm kibana-4.5.1-1.x86_64.r ...
- zepto触摸事件解决方法
移动项目开发过程中,经常需要用到滑动的事件来处理一些效果.通常情况下,我们会通过 touchstart->touchmove->touchend 的过程来定义这个事件.这些事件的触发顺 ...
- 【转】 GridView 72般绝技
说明:准备出一个系列,所谓精髓讲C#语言要点.这个系列没有先后顺序,不过尽量做到精.可能会不断增删整理,本系列最原始出处是csdn博客,谢谢关注. C#精髓 第四讲 GridView 72般绝技 作者 ...
- 创建第一个JBPM6项目并且运行自带的helloword例子(JBPM6学习之三)
1. 打开Eclipse,右键New JBPM Project 项目,在项目名称里面填写一个项目名字,如“TestJbpm6”,然后下一步,知道Finish完成(里面会使用我们配置的运行环境). 2. ...
- Android中的“再按一次返回键退出程序”实现
用户退出应用前给出一个提示是很有必要的,因为可能是用户并不真的想退出,而只是一不小心按下了返回键,大部分应用的做法是在应用退出去前给出一个Dialog,我觉得这样不太友好,用户还得移动手指去按dial ...
- PHP四种基础算法详解
许多人都说 算法是程序的核心,一个程序的好于差,关键是这个程序算法的优劣.作为一个初级phper,虽然很少接触到算法方面的东西 .但是对于冒泡排序,插入排序,选择排序,快速排序四种基本算法,我想还是要 ...
- jquery的change():下拉框值改变时触发
<script src="jquery.min.js" type="text/javascript"></script> <scr ...
- 7 HandlerSet 处理程序链表类——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 my.oschina.net/oloroso Handler ...