2013-09-27 2 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

왜 UTFont가 아닌 CTFontRef를 사용하고 있습니까? –

+0

UIFont를 사용할 수 있다는 것을 의미합니까? 방법? –

+0

'[UIFont fontWithName : @ "HelveticaNeue-Bold"size : sizeOfFonthighLightSomeStuff] 그러면 캐스트가 필요하지 않습니다. –

답변

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; 
} 
관련 문제