- (void)loadFont{// Get the path to our custom font and create a data provider.NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"mycustomfont" ofType:@"ttf"];CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);
// Create the font with the data provider, then release the data provider.customFont = CGFontCreateWithDataProvider(fontDataProvider);CGDataProviderRelease(fontDataProvider);}
现在,在你的drawRect:中,做这样的事情:
-(void)drawRect:(CGRect)rect{[super drawRect:rect];// Get the context.CGContextRef context = UIGraphicsGetCurrentContext();CGContextClearRect(context, rect);// Set the customFont to be the font used to draw.CGContextSetFont(context, customFont);
// Set how the context draws the font, what color, how big.CGContextSetTextDrawingMode(context, kCGTextFillStroke);CGContextSetFillColorWithColor(context, self.fontColor.CGColor);UIColor * strokeColor = [UIColor blackColor];CGContextSetStrokeColorWithColor(context, strokeColor.CGColor);CGContextSetFontSize(context, 48.0f);
// Create an array of Glyph's the size of text that will be drawn.CGGlyph textToPrint[[self.theText length]];
// Loop through the entire length of the text.for (int i = 0; i < [self.theText length]; ++i) {// Store each letter in a Glyph and subtract the MagicNumber to get appropriate value.textToPrint[i] = [[self.theText uppercaseString] characterAtIndex:i] + 3 - 32;}CGAffineTransform textTransform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);CGContextSetTextMatrix(context, textTransform);CGContextShowGlyphsAtPoint(context, 20, 50, textToPrint, [self.theText length]);}
func allFonts(){
for family in UIFont.familyNames(){
println(family)
for name in UIFont.fontNamesForFamilyName(family.description){println(" \(name)")}
}
}
for family: String in UIFont.familyNames(){print("\(family)")for names: String in UIFont.fontNamesForFamilyName(family){print("== \(names)")}}
目标c
for (NSString* family in [UIFont familyNames]){NSLog(@"%@", family);for (NSString* name in [UIFont fontNamesForFamilyName: family]){NSLog(@" %@", name);}}