2013-09-27 189 views
1
-(NSMutableAttributedString *) masBlueBoldColorString:(NSString *) theString 
{ 



    UIFont * italicSystemFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:sizeOfFonthighLightSomeStuff]; 


    CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)italicSystemFont.fontName, italicSystemFont.pointSize, NULL); 
    NSRange rangeHighlight = NSMakeRange(0, theString.length); 

    NSMutableAttributedString * mutableAttributedString = [[ NSMutableAttributedString alloc]initWithString:theString]; 

    if (font) { 
     [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:rangeHighlight]; 
     CFRelease(font); 
    } 

    return mutableAttributedString; 
} 

有一些問題,此代碼....如何讓這段代碼更優雅?

我想要的「字體」通過一個方法返回。我們有CFAutoRelease嗎?如果我們根本不需要使用字體,它會更好

+1

你爲什麼使用CTFontRef而不是UIFont? –

+0

你的意思是我可以使用UIFont?怎麼樣? –

+0

'[UIFont fontWithName:@「HelveticaNeue-Bold」size:sizeOfFontthighLightSomeStuff]' 然後你不需要演員。 –

回答

4
- (UIFont*)myFontMethod 
{ 
    return [UIFont fontWithName:@"HelveticaNeue-Bold" size:sizeOfFonthighLightSomeStuff]; 
} 

-(NSMutableAttributedString *) masBlueBoldColorString:(NSString *) theString 
{ 
    UIFont * font = [self myFontMethod]; 

    NSMutableAttributedString * mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:theString]; 

    if (font) { 
     [mutableAttributedString addAttribute:NSFontNameAttribute value:font]; 
    } 

    return mutableAttributedString; 
}