iOS - Using TTTAttributedLabel to set two color text
[Objective-C][Library]TTTAttributedLabelをつかってみた!
1. 下載程式碼
2. 將TTTAttributedLabel.h 與 TTTAttributedLabel.m加到你的專案中。
#import "TTTAttributedLabel.h"
3. 有開ARC 使用第三方的函數庫時 在Targets設定中的Build Phases選擇TTTAttributedLabel.m,按下Enter鍵並且輸入「-fno-objc-arc」關閉使用ARC
4. 加入CoreText.framework 到你的專案中。
#import <CoreText/CoreText.h>
Have you included CoreText.framework in your project?
5. 在你需要兩種以上顏色的UILabel換成TTTAttributedLabel
#pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // Configure the cell... cell = [self getCellContentView: CellIdentifier]; TTTAttributedLabel *masterLabel = ((TTTAttributedLabel *)[cell viewWithTag: 2]); NSString* text = @"這是我的名片,請多指教。"; NSMutableAttributedString * tttstring = [[NSMutableAttributedString alloc] initWithString:text]; //字型與字體大小 NSRange textRange = [text rangeOfString:text]; UIFont *textSystemFont = [UIFont fontWithName: @"HeiTi TC" size: 18] ; CTFontRef italicFont = CTFontCreateWithName((__bridge CFStringRef)textSystemFont.fontName, textSystemFont.pointSize, NULL); if(italicFont){ [tttstring addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)italicFont range:textRange]; } CFRelease(italicFont); //紅字 NSRange redRange = [text rangeOfString:@"名片"]; [tttstring addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor redColor] CGColor] range:redRange]; //底線 NSRange underlinRange = [text rangeOfString:@"請多指教"]; [tttstring addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:[NSNumber numberWithBool:YES] range:underlinRange]; //組體 UIFont *boldSystemFont = [UIFont boldSystemFontOfSize: 18] ; CTFontRef boldFont = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL); if(boldFont){ [tttstring addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)boldFont range:underlinRange]; } CFRelease(boldFont); masterLabel.frame = MASTER_RECT; masterLabel.numberOfLines = 1; [masterLabel setBackgroundColor:[UIColor clearColor]];//clearTextBackgroundColor! [masterLabel setText:tttstring]; return cell; }
#pragma mark - #pragma mark custom cell view - (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier { CGRect cellFrame = (self.mainTableView.style == UITableViewStylePlain)? CGRectMake(0, 0, 320, 58) : CGRectMake(0, 0, 300, 58); UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifier]; cell.frame = cellFrame; TTTAttributedLabel *master = [[TTTAttributedLabel alloc] initWithFrame: MASTER_RECT]; master.tag = 2; master.numberOfLines = 1; master.lineBreakMode = UILineBreakModeWordWrap; [master setFont: [UIFont fontWithName: @"HeiTi TC" size: 18]]; [cell.contentView addSubview: master]; return cell; }
結果顯示
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。