常規(guī)字符串NSString
字符串是否包含子串
// 利用range
[dateStr rangeOfString:@"an"].location == NSNotFound
// iOS 8.0 later
[dateStr containsString:@"an"];
判斷只有數(shù)字、小數(shù)點(diǎn)和減號
#define NUMBERS @"0123456789.-"
#pragma mark -是否只包含數(shù)字,小數(shù)點(diǎn)显晶,負(fù)號
- (BOOL)isOnlyhasNumberAndpointWithString:(NSString *)string
{
NSCharacterSet *cs=[[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
NSString *filter=[[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filter];
}
字符串截取
- [str substringWithRange:NSMakeRange(m,n)] 從第m個(gè)開始截取n個(gè)
- [str substringToIndex:n] 截取到第n個(gè)(n不在截取內(nèi))
- [str substringFromIndex:n] 從第n個(gè)截取到末尾(包含n)
富文本NSMutableAttributedString
富文本屬性
NSFontAttributeName : 字體,默認(rèn)字體Helvetica(Neue) /字號12
NSForegroundColorAttributeNam : 字體顏色,取值為UIColor對象,默認(rèn)黑色
NSBackgroundColorAttributeName : 字體區(qū)域背景色,取值為UIColor對象,默認(rèn)nil,透明色
NSLigatureAttributeName : 連體屬性,取值為NSNumber對象(整數(shù)),0沒有連體字符,默認(rèn)1連體字符
NSKernAttributeName : 字符間距,取值為NSNumber 對象(整數(shù)),正值間距加寬,負(fù)值變窄
NSStrikethroughStyleAttributeName : 刪除線,取值為 NSNumber 對象(整數(shù))
NSStrikethroughColorAttributeName : 刪除線顏色,取值為UIColor對象,默認(rèn)黑色
NSUnderlineStyleAttributeName : 下劃線,取值為枚舉NSUnderlineStyle中的值,與刪除線類似
NSUnderlineColorAttributeName : 下劃線顏色,取值為UIColor 對象,默認(rèn)黑色
NSStrokeWidthAttributeName : 筆畫寬度,取值為NSNumber 對象(整數(shù)),負(fù)值填充效果,正值中空效果
NSStrokeColorAttributeName : 填充部分顏色,不是字體顏色,取值為UIColor 對象
NSShadowAttributeName : 陰影屬性,取值為NSShadow對象
NSTextEffectAttributeName : 文本特殊效果,取值為NSString對象,目前只有圖版印刷效果可用:
NSBaselineOffsetAttributeName : 基線偏移值,取值為NSNumber(float),正值上偏,負(fù)值下偏
NSObliquenessAttributeName : 字形傾斜度,取值為NSNumber(float),正值右傾,負(fù)值左傾
NSExpansionAttributeName : 文本橫向拉伸屬性,取值為NSNumber(float),正值橫向拉伸文本泰佳,負(fù)值橫向壓縮文本
NSWritingDirectionAttributeName : 文字書寫方向,從左向右書寫或者從右向左書寫
NSVerticalGlyphFormAttributeName : 文字排版方向,取值為NSNumber對象(整數(shù)),0表示橫排文本,1表示豎排文本
NSLinkAttributeName : 鏈接屬性,點(diǎn)擊后調(diào)用瀏覽器打開指定URL地址
NSAttachmentAttributeName : 文本附件,取值為NSTextAttachment對象,常用于文字圖片混排
NSParagraphStyleAttributeName : 文本段落排版格式,取值為NSParagraphStyle對象
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:str];
//格式調(diào)整
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
/**調(diào)行間距*/
style.lineSpacing = 10;
//字間距
[attStr addAttribute:NSKernAttributeName value:@5 range:NSMakeRange(0, [str length])];
//添加行間距
[attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [str length])];
//橫豎排版 ---無效果
// [attStr addAttribute:NSVerticalGlyphFormAttributeName value:@1 range:NSMakeRange(0, [str length])];
//下畫線
[attStr addAttribute:NSUnderlineStyleAttributeName value:@1 range:NSMakeRange(0, [str length])];
//邊線寬度
[attStr addAttribute:NSStrokeWidthAttributeName value:@1 range:NSMakeRange(10, 10)];
//邊線顏色
[attStr addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(10, 10)];
//陰影--無效果
[attStr addAttribute:NSShadowAttributeName value:@4 range:NSMakeRange(20, 10)];
//下劃線
[attStr addAttribute:NSStrikethroughStyleAttributeName value:@2 range:NSMakeRange(20, 10)];
[attStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(20, 10)];
//字體背影色
[attStr addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(45, 10)];
//字體顏色
[attStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(60, 10)];
//段落
[attStr addAttribute:NSParagraphStyleAttributeName value:@6 range:NSMakeRange(70, 80)];
富文本計(jì)算高度
#pragma mark 文字高度
- (CGFloat)textH:(NSString *)text lineSpace:(CGFloat)lineSpace width:(CGFloat)width font:(UIFont *)font
{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineSpacing = lineSpace;
NSDictionary *attributes = @{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:font};
CGSize size = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil].size;
return size.height;
}
改變子串顏色
+ (__kindof NSMutableAttributedString *_Nonnull)changeTotalString:(NSString *_Nonnull)totalString subStringArray:(NSArray *_Nonnull)subStringArray subStringColor:(UIColor *_Nonnull)color andFont:(UIFont *_Nonnull)font {
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
for (NSString *rangeStr in subStringArray) {
NSRange range = [totalString rangeOfString:rangeStr options:NSBackwardsSearch];
[attributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
[attributedStr addAttribute:NSFontAttributeName value:font range:range];
}
return attributedStr;
}
字符串中添加圖片
- (NSAttributedString *)stringWithUIImage:(NSString *) contentStr {
// 創(chuàng)建一個(gè)富文本
NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:contentStr];
// 修改富文本中的不同文字的樣式
[attriStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 5)];
[attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 5)];
/**
添加圖片到指定的位置
*/
NSTextAttachment *attchImage = [[NSTextAttachment alloc] init];
// 表情圖片
attchImage.image = [UIImage imageNamed:@"jiedu"];
// 設(shè)置圖片大小
attchImage.bounds = CGRectMake(0, 0, 40, 15);
NSAttributedString *stringImage = [NSAttributedString attributedStringWithAttachment:attchImage];
[attriStr insertAttributedString:stringImage atIndex:2];
// 設(shè)置數(shù)字為紅色
/*
[attriStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 9)];
[attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(5, 9)];
*/
//NSDictionary * attrDict = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],
// NSForegroundColorAttributeName: [UIColor blueColor] };
//創(chuàng)建 NSAttributedString 并賦值
//_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict];
NSDictionary * attriBute = @{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:30]};
[attriStr addAttributes:attriBute range:NSMakeRange(5, 9)];
// 添加表情到最后一位
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
// 表情圖片
attch.image = [UIImage imageNamed:@"jiedu"];
// 設(shè)置圖片大小
attch.bounds = CGRectMake(0, 0, 40, 15);
// 創(chuàng)建帶有圖片的富文本
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
[attriStr appendAttributedString:string];
return attriStr;
}
段落
// paragraph style
_style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
_style.alignment = NSTextAlignmentLeft; //對齊
_style.headIndent = 0.0f;//行首縮進(jìn)
_style.firstLineHeadIndent = 10.0f;//首行縮進(jìn)
_style.tailIndent = 0.0f;//行尾縮進(jìn)
_style.lineSpacing = 2.0f;//行間距
NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:_text attributes:@{ NSParagraphStyleAttributeName : _style}];
UILabel *_content = [[UILabel alloc] init];
_content.attributedText = attrText;
URL中含中文的解決
// 方法1:
NSString* encodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// 方法2:
NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)urlString,NULL,NULL,kCFStringEncodingUTF8);
如果URL含有已轉(zhuǎn)義的%等符號時(shí),會再次轉(zhuǎn)義而導(dǎo)致錯(cuò)誤.
查看方法2參數(shù)說明:
CFStringRef CFURLCreateStringByAddingPercentEscapes(CFAllocatorRef allocator,CFStringRef originalString, CFStringRef charactersToLeaveUnescaped, CFStringRef legalURLCharactersToBeEscaped,CFStringEncoding encoding);
因此做出修改,寫出方法:
NSString *encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)urlString, (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]", NULL, kCFStringEncodingUTF8);