15

UIKit 내에서 글꼴의 문자 간격/자간을 어떻게 바꿀 수 있는지에 대한 정보는 잠시 동안 인터넷을 수색했습니다.Cocoa Touch로 글꼴의 문자 간격/자간을 변경할 수 있습니까?

내 자신의 맞춤 글꼴을 사용하는 것처럼 내 마음이 두려워 할 수 없습니다. 어느 것이 끔찍한 소식일까요?

나는 애플이 이러한 제약으로 나쁜 디자인으로부터 우리를 보호하고 있다는 것을 알고있다. 그러나 그들은 또한 우리가 정말로 훌륭한 디자인을 구현하는 것을 방해하고있다.

사람들은 내가 뭘한다고 제안합니까?

  1. 글꼴을 표준 커닝과 함께 사용하면 사람이 차이를 눈치 채지 못할 것입니다.

  2. 힘들게 벌어 들인 현금으로 이별 한 후 사용자가 누릴 수있는 표정을 얻으려고 해킹 된 수업을 찾으십시오.

  3. 내가 간과 한 방식으로 UIKit을 사용하여 숨겨진 지식의 지식을 전하는 사람에게 영원한 감사를 다짐합니다.

+0

는 (공개 API의 방법 중 어느 것도 실제로 UIFont 노출 없지만) 아이폰에 글꼴을로드 실제로 수많은 방법이 있습니다 : http://stackoverflow.com/questions/360751/can-i-embed-a -custom-font-in-an-iPhone-application – rpetrich

+0

나는 애플이 CSS를 따라하기를 바란다. –

답변

3

Quartz 2D를 사용하여 필요한 것을 할 수 있습니다. 특히 CGContextSetCharacterSpacing 속성은 문자 사이의 간격을 제어 할 수 있습니다. 나는 커닝에 대해 잘 모르겠습니다.

Quartz 2D Programming Guide: Text

3

나는 문자 간격을 변경하려면 UILabel의 확장했습니다. 이 상자를 작동하고 UILabel 자체 (적절한 코딩!)에서 글꼴, 텍스트, 색 등을 가져와야합니다.

처음에는 선명한 색으로 텍스트를 두 번 그릴 수 있습니다. 레이블의 텍스트를 자동 가운데 맞추기위한 것입니다. 이것이 비효율적 일 수는 있겠지만 자동 중심에 배치하는 것이 좋지 않습니까?

즐기십시오!

@interface RALabel : UILabel { 
} 
@end 

@implementation RALabel 

- (void) drawRect:(CGRect)rect 
{ 

    // Drawing code 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSelectFont (context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], self.font.pointSize, kCGEncodingMacRoman); 
    CGContextSetCharacterSpacing(context, 1); 
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]); 
    CGAffineTransform myTextTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f); 
    CGContextSetTextMatrix (context, myTextTransform); 

    // draw 1 but invisbly to get the string length. 
    CGPoint p =CGContextGetTextPosition(context); 
    float centeredY = (self.font.pointSize + (self.frame.size.height- self.font.pointSize)/2)-2; 
    CGContextShowTextAtPoint(context, 0, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]); 
    CGPoint v =CGContextGetTextPosition(context); 

    // calculate width and draw second one. 
    float width = v.x - p.x; 
    float centeredX =(self.frame.size.width- width)/2; 
    CGContextSetFillColorWithColor(context, [self.textColor CGColor]); 
    CGContextShowTextAtPoint(context, centeredX, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]); 

} 
4

Google에서 UITextField로 동일한 작업을 수행하려고 여기에서 착륙했습니다. 아래의 UILabel에서 일반적으로 사용할 수있는 기능을 구현했습니다.

@interface AdjustableUILable : UILabel { 
    CGFloat characterSpacing; 
} 

@property CGFloat characterSpacing; 

@end 


@implementation AdjustableUILable 

@synthesize characterSpacing; 

- (void)drawTextInRect:(CGRect)rect 
{  
    if (characterSpacing) 
    { 
     // Drawing code 
     CGContextRef context = UIGraphicsGetCurrentContext(); 
     CGFloat size = self.font.pointSize; 

     CGContextSelectFont (context, [self.font.fontName UTF8String], size, kCGEncodingMacRoman); 
     CGContextSetCharacterSpacing (context, characterSpacing); 
     CGContextSetTextDrawingMode (context, kCGTextFill); 

     // Rotate text to not be upside down 
     CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0); 
     CGContextSetTextMatrix(context, xform); 
     const char *cStr = [self.text UTF8String]; 
     CGContextShowTextAtPoint (context, rect.origin.x, rect.origin.y + size, cStr, strlen(cStr)); 
    } 
    else 
    { 
     // no character spacing provided so do normal drawing 
     [super drawTextInRect:rect]; 
    } 
} 

@end 

그리고 그것을 사용하는

, 그냥있는 viewDidLoad에서 레이블 또는 뭔가

- (void)viewDidLoad 
{ 
     myLabel.characterSpacing = 2; // point size 
} 

내가 UITextField에 그것을 시도를 설정하지만, 사용자가 필드를 편집 한 후 불행하게도 그것은 단지 문자 간격을 추가합니다. -drawTextInRect는 -textFieldShouldEndEditing 대리자 메서드 후에 만 ​​호출됩니다. 다른 포럼에서는 커서를 표시하기 위해 UITextField가 글꼴 렌더링을 알아야하기 때문에 일부 제안했습니다. 그 조각에 대한 해결책을 찾지 못했습니다 ...

4

대답은 많은 서식을 잃어 버렸고 user363349의 대답은 대부분 나를 위해 일했지만 텍스트 정렬이 작동하지 않았습니다.

- (void) drawRect:(CGRect)rect 
{ 
    // Drawing code 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSelectFont (context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], self.font.pointSize, kCGEncodingMacRoman); 
    CGContextSetCharacterSpacing(context, characterSpacing); 
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]); 
    CGAffineTransform myTextTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f); 

    CGContextSetTextMatrix (context, myTextTransform); 

    // draw 1 but invisbly to get the string length. 
    CGPoint p =CGContextGetTextPosition(context); 
    float centeredY = (self.font.pointSize + (self.frame.size.height- self.font.pointSize)/2)-2; 
    CGContextShowTextAtPoint(context, 0, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]); 
    CGPoint v =CGContextGetTextPosition(context); 

    // calculate width and draw second one. 
    float width = v.x - p.x; 
    float destX = 0; 

    // calculate X position based on alignment 
    if([self textAlignment] == UITextAlignmentLeft){ 
     destX = 0; 
    }else if([self textAlignment] == UITextAlignmentCenter){ 
     destX = (self.frame.size.width- width)/2; 
    }else if([self textAlignment] == UITextAlignmentRight){ 
     destX = self.frame.size.width - width; 
    } 
    CGContextSetFillColorWithColor(context, [self.textColor CGColor]); 
    CGContextShowTextAtPoint(context, destX, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]); 
} 
+0

좋은, 정렬을 유지하는 것은 큰 추가입니다! –

1

CGContextShowText 메서드의 주된 문제는 ASCII 문자열 만 표시한다는 것입니다. UTF 문자열을 표시하려면 문자열 심볼을 글리프로 매핑하고 글리프를 표시해야합니다.

CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSelectFont (context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], self.font.pointSize, kCGEncodingMacRoman); 
    CGContextSetCharacterSpacing(context, characterSpacing); 
    CGContextSetFillColorWithColor(context, [self.textColor CGColor]); 
    CGAffineTransform myTextTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f); 
    CGContextSetTextMatrix (context, myTextTransform); 

    CGGlyph glyphs[self.text.length]; 
    CTFontRef fontRef = CTFontCreateWithName((CFStringRef)self.font.fontName, self.font.pointSize, NULL); 
    CTFontGetGlyphsForCharacters(fontRef, (const unichar*)[self.text cStringUsingEncoding:NSUnicodeStringEncoding], glyphs, self.text.length); 
    float centeredY = (self.font.pointSize + (self.frame.size.height- self.font.pointSize)/2)-2; 
    CGContextShowGlyphsAtPoint(context, rect.origin.x, centeredY, (const CGGlyph *)glyphs, self.text.length); 
관련 문제