2011-09-04 11 views
8

मैं आईओएस में एक कस्टम फ़ॉन्ट का उपयोग करने के लिए CATextLayer का उपयोग कर रहा हूं, मुझे पता है कि Fonts provided by application के साथ कस्टम फ़ॉन्ट का उपयोग करने का एक आसान तरीका है लेकिन यह अलग फ़ॉन्ट है। मैं सोच रहा था कि प्रत्येक पात्र के बीच अंतर बदलने का कोई तरीका है? मुझे ऐसा करने के लिए कोई संपत्ति नहीं मिली!कैटक्स्टलेयर और अक्षरों के बीच ट्रैकिंग/अंतर

संपादित:

- (void)viewWillAppear:(BOOL)animated { 
    CTFontRef font = [self newCustomFontWithName:@"yeki" 
              ofType:@"ttf" 
             attributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:16.f] 
                      forKey:(NSString *)kCTFontSizeAttribute]]; 


    CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

    normalTextLayer_ = [[CATextLayer alloc] init]; 
    normalTextLayer_.font = font; 
    normalTextLayer_.string = str; 
    normalTextLayer_.wrapped = YES; 
    normalTextLayer_.foregroundColor = [[UIColor purpleColor] CGColor]; 
    normalTextLayer_.fontSize = 50.f; 
    normalTextLayer_.alignmentMode = kCAAlignmentRight; 

    normalTextLayer_.frame = CGRectMake(0.f,100.f, screenBounds.size.width, screenBounds.size.height /1.f); 
    [self.view.layer addSublayer:normalTextLayer_]; 
    CFRelease(font); 
} 
+2

आपका मतलब कर्नाई है? आप 'CGContextSetCharacterSpacing' का उपयोग करने का प्रयास कर सकते हैं। –

+0

हां, लेकिन मुझे CATextLayer क्लास के लिए 'CGContextSetCharacterSpacing' के साथ कोई समाधान नहीं मिला! –

+0

कोई प्रतिक्रिया ???? –

उत्तर

12

आप एक NSAttributedString (या NSMutableAttributedString) के बजाय एक सादे NSString परत को असाइन करने और (Core Text String Attributes Reference देखें) कर्निंग समायोजित करने के लिए kCTKernAttributeName विशेषता का उपयोग कर सकते हैं।

उदाहरण:

CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica"), 30, NULL); 
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
          (id)font, kCTFontAttributeName, 
          [NSNumber numberWithFloat:15.0], kCTKernAttributeName, 
          (id)[[UIColor greenColor] CGColor], kCTForegroundColorAttributeName, nil]; 
NSAttributedString *attributedString = [[[NSAttributedString alloc] initWithString:@"Hello World" attributes:attributes] autorelease]; 
CFRelease(font); 
myTextLayer.string = attributedString; 

है कि आप वृद्धि चरित्र अंतर के साथ Helvetica 30 में हरे पाठ देना चाहिए।

+0

धन्यवाद। और क्या आप कृपया यह बताएं कि मैं इसका उपयोग कैसे कर सकता हूं: 'kCTKernAttributeName' क्योंकि जब मैं अपने CATextLayer स्ट्रिंग को' NSAttributedString 'के साथ प्रतिस्थापित करता हूं, तो मेरे पाठ रंग, कस्टम फ़ॉन्ट्स आदि जैसे कुछ प्रभाव खो देते हैं ... –

+0

कृपया मेरा संपादित कोड देखने के लिए मेरा संपादित प्रश्न देखें : –

+0

'CATextLayer' का रंग और फ़ॉन्ट विशेषता केवल सादा तारों पर लागू होती है क्योंकि एक 'एनएसएट्रिब्यूटेड स्ट्रिंग' अपने गुणों को परिभाषित करता है जो स्ट्रिंग के भीतर विभिन्न श्रेणियों पर लागू हो सकते हैं (विभिन्न वर्णों में अलग-अलग रंग हो सकते हैं ...)। आप संपूर्ण स्ट्रिंग पर लागू विशेषताओं के शब्दकोश के साथ जिम्मेदार स्ट्रिंग को प्रारंभ करने के लिए 'initWithString: विशेषताएँ' का उपयोग कर सकते हैं। उपलब्ध गुणों के अवलोकन के लिए मैंने जो दस्तावेज लिंक किया है उसे देखें। – omz