2012年10月7日

iOS -- different fonts and colors in a NSString 字串裡可以有不同的字體和顏色



讓某一個 NSString 裡可以有不同的 font 和顏色

                 



iOS 3.2 以後   iOS 6 以前  如果要達到這樣的效果,可以試一試下列的步驟:

1. 加入 CoreText.framework
2、匯入 TTTAttributedLabel.h  和  TTTAttributedLabel.m 到你的 Xcode Project
3、不要使用 UILabel; 改用 TTTAttributedLabel 的 class



QQViewController.h


#import <UIKit/UIKit.h>
#import "TTTAttributedLabel.h"

@interface QQViewController : UIViewController 
@property (strong, nonatomic) TTTAttributedLabel *label;

@end

QQViewController.m
#import "QQViewController.h"

@implementation QQViewController
@synthesize label;

- (void)viewDidLoad
{
    [super viewDidLoad];

//定義標籤框Label 的一些參數
    label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(10, 20, 200, 100)];
    label.font = [UIFont systemFontOfSize:18];
    label.textColor = [UIColor blueColor];
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.numberOfLines = 0;
    
    NSString *text = @"Lorem ipsum dolar sit amet";
    

    [label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {

    //SET 3 different RANGES  設定一字串中有三個不同的範圍
    NSRange frontRange = [[mutableAttributedString string] rangeOfString:@"Lor" options:NSCaseInsensitiveSearch];
        
    NSRange boldRange = [[mutableAttributedString string] rangeOfString:@"ipsum dolar" options:
            NSCaseInsensitiveSearch];

    NSRange aStressRange = NSMakeRange(24, 1);

        //SET FONT 設定字體
        //粗體
        UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:18];
        CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
        //斜體
        UIFont *italicSystemFont = [UIFont italicSystemFontOfSize:18];
        CTFontRef italicFont = CTFontCreateWithName((__bridge CFStringRef)italicSystemFont.fontName, italicSystemFont.pointSize, NULL);
        
        if (font) {
            //第一段範圍的字體
            [mutableAttributedString removeAttribute:(NSString *)kCTFontAttributeName 
              range:frontRange];//prevent memory leak
            [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:
              (__bridge id)italicFont range:frontRange];
            
            //第二段範圍的字體
            [mutableAttributedString removeAttribute:(NSString *)kCTFontAttributeName 
              range:boldRange];//prevent memory leak
            [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:
            (__bridge id)font range:boldRange];
           
            //第三段範圍的字體
            [mutableAttributedString removeAttribute:(NSString *)kCTFontAttributeName 
              range:aStressRange];//prevent memory leak
            [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:
            (__bridge id)font range:aStressRange];
            
            //自定字體
            // [mutableAttributedString addAttribute:@"TTTCustomStrikeOut" value:[NSNumber
            //  numberWithBool:YES] range:aStressRange];
            
            CFRelease(font);
        }  //end of font

        
       //SET COLOR
        //第一段範圍的字體顏色
        [mutableAttributedString addAttribute:(NSString *) kCTForegroundColorAttributeName 
          value:(id)[[UIColor greenColor] CGColor] range:frontRange];


        //第二段範圍的字體顏色
         [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName 
          value:(id)[[UIColor grayColor] CGColor] range:boldRange];
        
         //第三段範圍的字體顏色
        [mutableAttributedString addAttribute:(NSString *) kCTForegroundColorAttributeName 
          value:(id) [[UIColor redColor] CGColor] range:aStressRange];
        
        
        //SET CAPITAL
        [mutableAttributedString replaceCharactersInRange:boldRange withString:
          [[[mutableAttributedString string] substringWithRange:boldRange] uppercaseString]];
        
        return mutableAttributedString;
    }];
    
    [self.view addSubview:label];
    
    
    
}






沒有留言:

張貼留言