如何让这段代码更优雅?

问题描述:

-(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]' 然后你不需要演员。 –

- (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; 
}