生活随笔
收集整理的這篇文章主要介紹了
CoreText入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
學習了網上的一些CoreText知識之后,總結了一下學習成果,自定義了一個Label控件。
Label特點:
1、可以設置文本中某些字段為關鍵字;
2、可以設置文本顏色和其中關鍵字的顏色;
3、可以設置文本下劃線樣式和其中關鍵字下劃線樣式;
4、可以指定文本字體與大小和其中關鍵字的字體與大小;
5、可以響應關鍵字點擊;
說明一下其中的關鍵類的意義:
CHLabel.h 自定義的控件,繼承自UILabel;
TestViewController.h 是項目的根視圖控制器,其中使用了CHLabel;
下面看代碼:
CHLabel 代碼實現:
[cpp] view plaincopyprint?
#import?<UIKit/UIKit.h>??#import<CoreText/CoreText.h>????typedef?enum??{??????kCHLabelUnderlineStyleNone?=?0?,??????kCHLabelUnderlineStyleSingle?,??????kCHLabelUnderlineStyleThick?,??????kCHLabelUnderlineStyleDouble???}?CHLabelUnderlineStyle;????@protocol?CHLabelDelegate;??@interface?CHLabel?:?UILabel??{??????NSString??????????????????*_textCH;????????????????????UIColor???????????????????*_textColorCH;???????????????UIFont????????????????????*_textFontCH;????????????????CHLabelUnderlineStyle??????_textUnderlineStyle;??????????????NSString??????????????????*_textKeyWordCH;?????????????UIColor???????????????????*_textKeyWordColorCH;????????UIFont????????????????????*_keyWordFontCH;?????????????CHLabelUnderlineStyle??????_keyWordUnderlineStyle;????????????NSMutableArray????????????*_keyWordsCH;????????????????NSMutableAttributedString?*_attributedString;??????}????@property?(nonatomic,???copy)?NSString??????????????????*textCH;??@property?(nonatomic,?retain)?UIColor???????????????????*textColorCH;??@property?(nonatomic,???copy)?NSString??????????????????*textKeyWordCH;??@property?(nonatomic,?retain)?UIColor???????????????????*textKeyWordColorCH;??@property?(nonatomic,?retain)?UIFont????????????????????*textFontCH;??@property?(nonatomic,?retain)?UIFont????????????????????*keyWordFontCH;???@property?(nonatomic,?retain)?NSMutableArray????????????*keyWordsCH;??@property?(nonatomic,?retain)?NSMutableAttributedString?*attributedString;??@property?(nonatomic,?retain)?id<CHLabelDelegate>????????delegate;????-?(void)?setText:(NSString?*)?textString?andKeyWord:(NSString?*)?keyWord;????-?(void)?setTextColor:(UIColor?*)textColor?andKeyWordColor:(UIColor?*)?keyWordColor;????-?(void)?setTextFont:?(UIFont?*)textFont?andKeyWordFont:(UIFont?*)?keyWordFont;????-?(void)?setTextUnderlineStyle:?(CHLabelUnderlineStyle)textUnderlineStyle?andKeyWordUnderlineStyle:(CHLabelUnderlineStyle)?keyWordUnderlineStyle;????@end????@protocol?CHLabelDelegate?<NSObject>??@optional??-?(void)?CHLabel:(CHLabel?*)?chLabel?tapOnKeyWord:(NSString?*)?keyWord;????@end??#import?"CHLabel.h"????@implementation?CHLabel????@synthesize?textCH???????????????=?_textCH;??@synthesize?textColorCH??????????=?_textColorCH;??@synthesize?textKeyWordCH????????=?_textKeyWordCH;??@synthesize?textKeyWordColorCH???=?_textKeyWordColorCH;??@synthesize?textFontCH???????????=?_textFontCH;??@synthesize?keyWordFontCH????????=?_keyWordFontCH;??@synthesize?keyWordsCH???????????=?_keyWordsCH;??@synthesize?attributedString?????=?_attributedString;??@synthesize?delegate;????void?safeRelease(id?pointer)??{??????if?(!pointer)??????{??????????[pointer?release];??????????pointer?=?nil;??????}??}????-?(void)?dealloc??{??????safeRelease(_textCH);??????safeRelease(_textColorCH);??????safeRelease(_textKeyWordCH);??????safeRelease(_textKeyWordColorCH);??????safeRelease(_textFontCH);??????safeRelease(_keyWordFontCH);??????safeRelease(_keyWordsCH);??????safeRelease(_attributedString);??????safeRelease(delegate);????????????[super?dealloc];??}????-?(void)?initializtion??{??????_textKeyWordCH?=?nil;??????_textKeyWordColorCH?=?nil;??????_keyWordsCH?=?[[NSMutableArray?alloc]?init];??}????-?(id)?init??{??????if?(self?=?[super?init])??????{??????????[self?initializtion];??????}??????return?self;??}????-?(id)initWithFrame:(CGRect)frame??{??????if?([super?initWithFrame:frame])??????{??????????[self?initializtion];??????}??????return?self;??}????-?(void)?setText:(NSString?*)?textString?andKeyWord:(NSString?*)?keyWord??{??????if?(self.text?!=?textString)??????{??????????self.text?=?textString;??????????self.textCH?=?textString;??????}????????????[self?fetchKeywordRange:keyWord];??}????-?(void)?setTextColor:(UIColor?*)textColor?andKeyWordColor:(UIColor?*)?keyWordColor??{??????self.textColorCH?=?textColor;??????self.textKeyWordColorCH?=?keyWordColor;??}????-?(void)?setTextFont:(UIFont?*)textFont?andKeyWordFont:(UIFont?*)?keyWordFont??{??????self.textFontCH?=?textFont;??????self.keyWordFontCH?=?keyWordFont;??}????-?(void)?setTextUnderlineStyle:?(CHLabelUnderlineStyle)textUnderlineStyle?andKeyWordUnderlineStyle:(CHLabelUnderlineStyle)?keyWordUnderlineStyle??{??????_textUnderlineStyle?=?textUnderlineStyle;??????_keyWordUnderlineStyle?=?keyWordUnderlineStyle;??}????-?(void)?fetchKeywordRange:(NSString?*)keyWord??{??????if?(nil?==?keyWord)?{??????????return;??????}????????????NSMutableAttributedString?*mutableAttributedString?=?[[NSMutableAttributedString?alloc]?initWithString:self.text];??????NSUInteger?count?=?0,?length?=?[mutableAttributedString?length];??????NSRange?range?=?NSMakeRange(0,?length);????????????count?=?0,?length?=?[mutableAttributedString?length];??????range?=?NSMakeRange(0,?length);????????????while(range.location?!=?NSNotFound)??????{??????????range?=?[[mutableAttributedString?string]?rangeOfString:keyWord?options:0?range:range];??????????if(range.location?!=?NSNotFound)?{???????????????????????NSValue?*value?=?[NSValue?valueWithRange:range];??????????????if?(range.length?>?0)?{??????????????????[self.keyWordsCH?addObject:value];??????????????}????????????????????????????range?=?NSMakeRange(range.location?+?range.length,?length?-?(range.location?+?range.length));??????????????count++;??????????}??????}??}????-?(int)?labelUnderlineType:(CHLabelUnderlineStyle)?lType??{??????int?underLineType;??????switch?(lType)?{??????????case?0:??????????????underLineType?=?kCTUnderlineStyleNone;??????????????break;??????????case?1:??????????????underLineType?=?kCTUnderlineStyleSingle;??????????????break;??????????case?2:??????????????underLineType?=?kCTUnderlineStyleThick;??????????????break;??????????case?3:??????????????underLineType?=?kCTUnderlineStyleDouble;??????????????break;??????????default:??????????????underLineType?=?kCTUnderlineStyleNone;??????????????break;??????}????????????return?underLineType;??}????-?(NSAttributedString?*)?richString:(NSString?*)?textString??{??????int?length?=?[textString?length];??????if?(self.attributedString)?{??????????self.attributedString?=?nil;??????}?????self.attributedString?=?[[NSMutableAttributedString?alloc]?initWithString:textString];??????[self.attributedString?addAttribute:(NSString?*)(kCTForegroundColorAttributeName)???????????????????????????????value:(id)self.textColorCH.CGColor???????????????????????????????range:NSMakeRange(0,?length)];????????????int?numType?=?0;??????CFNumberRef?cfNum?=?CFNumberCreate(NULL,?kCFNumberIntType,?&numType);??????[self.attributedString?addAttribute:(NSString?*)kCTLigatureAttributeName????????????????????????????????????value:(id)cfNum????????????????????????????????????range:NSMakeRange(0,?length)];????????????float?fNum?=3.0;??????CFNumberRef?cfNum2?=?CFNumberCreate(NULL,?kCFNumberFloatType,?&fNum);??????[self.attributedString?addAttribute:(NSString?*)(kCTStrokeColorAttributeName)????????????????????????????????????value:(id)cfNum2????????????????????????????????????range:NSMakeRange(0,?length)];????????????if?(!self.textFontCH)?{??????????self.textFontCH?=?self.font;??????}??????CTFontRef?ctFont?=?CTFontCreateWithName((CFStringRef)self.textFontCH.fontName,?self.textFontCH.pointSize,?NULL);??????[self.attributedString?addAttribute:(NSString?*)(kCTFontAttributeName)????????????????????????????????????value:(id)ctFont????????????????????????????????????range:NSMakeRange(0,?length)];????????????int?underLineType?=?[self?labelUnderlineType:_textUnderlineStyle];??????CFNumberRef?cfUnderLine?=?CFNumberCreate(NULL,?kCTUnderlineStyleThick,?&underLineType);??????[self.attributedString?addAttribute:(NSString?*)(kCTUnderlineStyleAttributeName)????????????????????????????????????value:(id)cfUnderLine????????????????????????????????????range:NSMakeRange(0,?length)];????????????if?(self.textKeyWordColorCH?!=?nil)??????{??????????for?(NSValue?*value?in?self.keyWordsCH)??????????{??????????????NSRange?keyRange?=?[value?rangeValue];??????????????[self.attributedString?addAttribute:(NSString?*)(kCTForegroundColorAttributeName)????????????????????????????????????????????value:(id)self.textKeyWordColorCH.CGColor????????????????????????????????????????????range:keyRange];????????????????????????????if?(!self.keyWordFontCH)?{??????????????????self.keyWordFontCH?=?self.font;??????????????}??????????????CTFontRef?ctFont?=?CTFontCreateWithName((CFStringRef)self.keyWordFontCH.fontName,?self.keyWordFontCH.pointSize,?NULL);??????????????[self.attributedString?addAttribute:(NSString?*)(kCTFontAttributeName)????????????????????????????????????????????value:(id)ctFont????????????????????????????????????????????range:keyRange];????????????????????????????int?underLineType?=?[self?labelUnderlineType:_keyWordUnderlineStyle];??????????????CFNumberRef?cfUnderLine?=?CFNumberCreate(NULL,?kCTUnderlineStyleThick,?&underLineType);??????????????[self.attributedString?addAttribute:(NSString?*)(kCTUnderlineStyleAttributeName)????????????????????????????????????????????value:(id)cfUnderLine????????????????????????????????????????????range:keyRange];????????????????????????????[self.attributedString?addAttribute:@"option"?value:[[self.attributedString?string]?substringWithRange:keyRange]?range:keyRange];??????????}??????}????????????return?[[self.attributedString?copy]?autorelease];??}????-?(void)?drawRect:(CGRect)rect??{??????CGContextRef?context?=?UIGraphicsGetCurrentContext();??????CGContextSaveGState(context);??????CGContextConcatCTM(context,?CGAffineTransformScale(CGAffineTransformMakeTranslation(0,?rect.size.height+6.5),?1.f,?-1.f));????????????CGContextSetTextPosition(context,?0.0,?0.0);??????CTFramesetterRef?framesetter?=?CTFramesetterCreateWithAttributedString((CFAttributedStringRef)?[self?richString:self.text]);????????????????????CGMutablePathRef?leftColumnPath?=?CGPathCreateMutable();??????CGPathAddRect(leftColumnPath,?NULL,?rect);??????CTFrameRef?leftFrame?=?CTFramesetterCreateFrame(framesetter,?CFRangeMake(0,?0),?leftColumnPath,?NULL);??????CTFrameDraw(leftFrame,?context);??????CGContextRestoreGState(context);??????CGPathRelease(leftColumnPath);??????CFRelease(framesetter);??}????-?(void)touchesBegan:(NSSet?*)touches?withEvent:(UIEvent?*)event?{????????????[super?touchesEnded:touches?withEvent:event];????????????CGPoint?tapLocation?=?[[touches?anyObject]?locationInView:self];??????int?total_height?=?[self?getAttributedStringHeightWithString:self.attributedString?WidthValue:self.frame.size.width];????????????if?(tapLocation.y?>=?total_height)?{??????????return;??????}??????????????????CTFramesetterRef?framesetter?=?CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.attributedString);????????????CGMutablePathRef?path?=?CGPathCreateMutable();??????CGPathAddRect(path,?NULL,?CGRectMake(0,?0,?self.frame.size.width,?1000));????????????CTFrameRef?textFrameForKey?=?CTFramesetterCreateFrame(framesetter,?CFRangeMake(0,?0),?path,?NULL);????????????CFRelease?(path);??????CFRelease?(framesetter);????????????NSArray?*linesArray?=?(NSArray?*)?CTFrameGetLines(textFrameForKey);????????????CGPoint?origins[[linesArray?count]];??????CTFrameGetLineOrigins(textFrameForKey,?CFRangeMake(0,?0),?origins);??????CFArrayRef?lines?=?CTFrameGetLines(textFrameForKey);????????????CGFloat?ascent;??????CGFloat?descent;??????CGFloat?leading;????????????CTLineRef?line?=?(CTLineRef)?[linesArray?objectAtIndex:0];??????CTLineGetTypographicBounds(line,?&ascent,?&descent,?&leading);????????????????????????int?line_y?=?1000-?(int)origins[0].y;????????int?line_height?=?line_y?+?(int)descent?+1;?????????????int?current_line?=?tapLocation.y/line_height;????????????CFIndex?curentIndex?=?CTLineGetStringIndexForPosition((CTLineRef)CFArrayGetValueAtIndex(lines,?current_line),tapLocation);??????????????????if?(curentIndex?>[self.attributedString?length])?{??????????return;??????}????????????NSRange?currentRange?=?NSMakeRange(0,?[self.attributedString?length]);????????????NSDictionary?*dic?=?[self.attributedString?attributesAtIndex:curentIndex-1?effectiveRange:¤tRange];??????id?option?=?[dic?valueForKey:@"option"];????????????if?(option)?{?????????????????if?([delegate?respondsToSelector:@selector(CHLabel:tapOnKeyWord:)])??????????{??????????????[delegate?CHLabel:self?tapOnKeyWord:(NSString?*)[dic?valueForKey:@"option"]];??????????}??????}??}??????-?(int)getAttributedStringHeightWithString:(NSAttributedString?*)string??WidthValue:(int)?width??{??????int?total_height?=?0;??????????????????CTFramesetterRef?framesetter?=?CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);????????????CGRect?drawingRect?=?CGRectMake(0,?0,?width,?1000);????????CGMutablePathRef?path?=?CGPathCreateMutable();??????CGPathAddRect(path,?NULL,?drawingRect);??????CTFrameRef?textFrame?=?CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0),?path,?NULL);??????CGPathRelease(path);??????CFRelease(framesetter);????????????NSArray?*linesArray?=?(NSArray?*)?CTFrameGetLines(textFrame);????????????CGPoint?origins[[linesArray?count]];??????CTFrameGetLineOrigins(textFrame,?CFRangeMake(0,?0),?origins);????????????int?line_y?=?(int)?origins[[linesArray?count]?-1].y;??????????????CGFloat?ascent;??????CGFloat?descent;??????CGFloat?leading;????????????CTLineRef?line?=?(CTLineRef)?[linesArray?objectAtIndex:[linesArray?count]-1];??????CTLineGetTypographicBounds(line,?&ascent,?&descent,?&leading);????????????total_height?=?1000?-?line_y?+?(int)?descent?+1;????????????????CFRelease(textFrame);????????????return?total_height;??}????@end??????TestViewController.h?代碼實現:??@interface?TestViewController?:?UIViewController?<CHLabelDelegate>??{??????IBOutlet?UITextField?*textString;??????IBOutlet?UITextField?*keyString;??}????-?(IBAction)?showText:(id)sender;????@end????#import?"TestViewController.h"????@interface?TestViewController?()????@end????@implementation?TestViewController????-?(id)initWithNibName:(NSString?*)nibNameOrNil?bundle:(NSBundle?*)nibBundleOrNil??{??????self?=?[super?initWithNibName:nibNameOrNil?bundle:nibBundleOrNil];??????if?(self)?{????????????????}??????return?self;??}????-?(IBAction)?showText:(id)sender??{??????[textString?resignFirstResponder];??????[keyString?resignFirstResponder];????????????CHLabel?*label?=?[[CHLabel?alloc]?initWithFrame:CGRectMake(10,?260,?300,?200)];????????????[label?setUserInteractionEnabled:YES];??????[label?setText:textString.text?andKeyWord:keyString.text];??????[label?setTextColor:[UIColor?redColor]?andKeyWordColor:[UIColor?blueColor]];??????[label?setTextUnderlineStyle:kCHLabelUnderlineStyleSingle?andKeyWordUnderlineStyle:kCHLabelUnderlineStyleDouble];??????[label?setTextFont:[UIFont?systemFontOfSize:20]?andKeyWordFont:[UIFont?boldSystemFontOfSize:30]];??????label.backgroundColor?=?[UIColor?lightGrayColor];??????[label?setNumberOfLines:0];??????label.delegate?=?self;??????????????????NSArray?*fontArray?=?[UIFont?familyNames];??????NSString?*fontName;??????if?([fontArray?count])?{??????????fontName?=?[fontArray?objectAtIndex:0];??????}??????[label?setFont:[UIFont?fontWithName:fontName?size:20]];????????????[self.view?addSubview:label];????????[label?release];??}????-?(void)viewDidLoad??{??????[super?viewDidLoad];??}????-?(void)didReceiveMemoryWarning??{??????[super?didReceiveMemoryWarning];??}????#pragma?mark?CHLabelDelegate??-?(void)?CHLabel:(CHLabel?*)?chLabel?tapOnKeyWord:(NSString?*)?keyWord??{??????UIAlertView?*alert?=?[[UIAlertView?alloc]?initWithTitle:@"notice"??????????????????????????????????????????????????????message:[NSString?stringWithFormat:@"Tap?on?keyWord:%@",?keyWord]?????????????????????????????????????????????????????delegate:nil????????????????????????????????????????????cancelButtonTitle:@"OK"????????????????????????????????????????????otherButtonTitles:nil];??????[alert?show];??????[alert?release];??
}
完整的項目鏈接:http://pan.baidu.com/share/link?shareid=362100&uk=3674861929
轉載請保留,原文鏈接:http://blog.csdn.net/zfpp25_/article/details/8639215
若發現有不合適或錯誤之處,還請批評指正,不勝感激。
總結
以上是生活随笔為你收集整理的CoreText入门的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。