程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 設置CoreText基本屬性

設置CoreText基本屬性

編輯:C++入門知識

通過以下代碼來學習一下coretext的基本屬性的設置,加深了解coretext是通過哪些基本的屬性來設置自己的樣式的  首先,繼承UIView,並重載其drawrect函數:   [cpp]   - (void)drawRect:(CGRect)rect {   // Drawing code.      //創建要輸出的字符串   NSString *longText = @”袁唯來來 Lorem ipsum dolor sit amet, Before the iPad was released you had basically two ways how to get text on screen. Either you would stick with UILabel or UITextView provided by UIKit or if you felt hard-core you would draw the text yourself on the Quartz level incurring all the headaches induced by having to mentally switch between Objective-C and C API functions.\   As of iOS 3.2 we gained a third alternative in Core Text promising full control over styles, thread safety and performance. However for most of my apps I did not want to break 3.x compatibility and so I procrastinated looking at this powerful new API. Apps running only on iPads could have made use of Core Text from day 1, but to me it made more sense supporting iPad via hybrid apps where the iPhone part would still be backwards compatible.\   Now as the year has turned the adoption of 4.x on all iOS platforms is ever more accelerating. Many new iPads where found under the Christmas tree and by now even the most stubborn people (read needing 3.x for jailbreaking and sim-unlocking) have little reason to stick with 3.x. Thus we have almost no incentive left to stick with 3.x compatibility. Yay!”; /* … */      //創建AttributeString   NSMutableAttributedString *string = [[NSMutableAttributedString alloc]   initWithString:longText];      //創建字體以及字體大小   CTFontRef helvetica = CTFontCreateWithName(CFSTR(”Helvetica”), 14.0, NULL);   CTFontRef helveticaBold = CTFontCreateWithName(CFSTR(”Helvetica-Bold”), 14.0, NULL);      //添加字體 目標字符串從下標0開始到字符串結尾   [string addAttribute:(id)kCTFontAttributeName   value:(id)helvetica   range:NSMakeRange(0, [string length])];      //添加字體 目標字符串從下標0開始,截止到4個單位的長度   [string addAttribute:(id)kCTFontAttributeName   value:(id)helveticaBold   range:NSMakeRange(0, 4)];      //添加字體 目標字符串從下標6開始,截止到5個單位長度   [string addAttribute:(id)kCTFontAttributeName   value:(id)helveticaBold   range:NSMakeRange(6, 5)];      //添加字體 目標字符串從下標109開始,截止到9個單位長度   [string addAttribute:(id)kCTFontAttributeName   value:(id)helveticaBold   range:NSMakeRange(109, 9)];      //添加字體 目標字符串從下標223開始,截止到6個單位長度   [string addAttribute:(id)kCTFontAttributeName   value:(id)helveticaBold   range:NSMakeRange(223, 6)];      //添加顏色,目標字符串從下標0開始,截止到4個單位長度   [string addAttribute:(id)kCTForegroundColorAttributeName   value:(id)[UIColor blueColor].CGColor   range:NSMakeRange(0, 4)];      //添加過程同上   [string addAttribute:(id)kCTForegroundColorAttributeName   value:(id)[UIColor redColor].CGColor   range:NSMakeRange(18, 3)];      [string addAttribute:(id)kCTForegroundColorAttributeName   value:(id)[UIColor greenColor].CGColor   range:NSMakeRange(657, 6)];      [string addAttribute:(id)kCTForegroundColorAttributeName   value:(id)[UIColor blueColor].CGColor   range:NSMakeRange(153, 6)];      //創建文本對齊方式   CTTextAlignment alignment = kCTLeftTextAlignment;//左對齊 kCTRightTextAlignment為右對齊   CTParagraphStyleSetting alignmentStyle;   alignmentStyle.spec=kCTParagraphStyleSpecifierAlignment;//指定為對齊屬性   alignmentStyle.valueSize=sizeof(alignment);   alignmentStyle.value=&alignment;      //創建文本行間距   CGFloat lineSpace=5.0f;//間距數據   CTParagraphStyleSetting lineSpaceStyle;   lineSpaceStyle.spec=kCTParagraphStyleSpecifierLineSpacing;//指定為行間距屬性   lineSpaceStyle.valueSize=sizeof(lineSpace);   lineSpaceStyle.value=&lineSpace;      //創建樣式數組   CTParagraphStyleSetting settings[]={   alignmentStyle,lineSpaceStyle   };      //設置樣式   CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings));      //給字符串添加樣式attribute   [string addAttribute:(id)kCTParagraphStyleAttributeName   value:(id)paragraphStyle   range:NSMakeRange(0, [string length])];      // layout master   CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(   (CFAttributedStringRef)string);      CGMutablePathRef leftColumnPath = CGPathCreateMutable();   CGPathAddRect(leftColumnPath, NULL,   CGRectMake(0, 0,   self.bounds.size.width,   self.bounds.size.height));      CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter,   CFRangeMake(0, 0),   leftColumnPath, NULL);      // flip the coordinate system   CGContextRef context = UIGraphicsGetCurrentContext();   CGContextSetTextMatrix(context, CGAffineTransformIdentity);   CGContextTranslateCTM(context, 0, self.bounds.size.height);   CGContextScaleCTM(context, 1.0, -1.0);      // draw   CTFrameDraw(leftFrame, context);      // cleanup      CGPathRelease(leftColumnPath);   CFRelease(framesetter);   CFRelease(helvetica);   CFRelease(helveticaBold);   [string release];   UIGraphicsPushContext(context);      }      

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved