Programing

iOS : 프로그래밍 방식으로 UILabel의 글꼴 크기 설정

crosscheck 2020. 10. 11. 09:07
반응형

iOS : 프로그래밍 방식으로 UILabel의 글꼴 크기 설정


UILabel의 글꼴 크기를 설정하려고합니다. 어떤 값을 넣어도 텍스트 크기는 변하지 않는 것 같습니다. 제가 사용하는 코드는 다음과 같습니다.

[self setTitleLabel:[[UILabel alloc] initWithFrame:CGRectMake(320.0,0.0,428.0,50.0)]];
[[self contentView] addSubview:[self titleLabel]];
UIColor *titlebg = [UIColor clearColor];
[[self titleLabel] setBackgroundColor:titlebg];
[[self titleLabel] setTextColor:[UIColor blackColor]];
[[self titleLabel] setFont:[UIFont fontWithName:@"System" size:36]];

시도 [UIFont systemFontOfSize:36]또는 [UIFont fontWithName:@"HelveticaNeue" size:36][[self titleLabel] setFont:[UIFont systemFontOfSize:36]];


목표 -C :

[label setFont: [label.font fontWithSize: sizeYouWant]];

빠른:

label.font = label.font.fontWithSize(sizeYouWant)

UILabel의 글꼴 크기 만 변경합니다.


신속한 코드를 찾고 있다면 :

var titleLabel = UILabel()
titleLabel.font = UIFont(name: "HelveticaNeue-UltraLight",
                         size: 20.0)

스위프트 3.0 / 스위프트 4.2 / 스위프트 5.0

labelName.font = labelName.font.withSize(15)

이 코드는 저에게 완벽하게 작동합니다.

  UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15,23, 350,22)];
  [label setFont:[UIFont systemFontOfSize:11]];

이름 @"System"이있는 글꼴 패밀리가 size:36없으므로 작동하지 않기 때문에 ...

속성 관리자의 xcode에서 사용할 수있는 글꼴을 확인하고


Swift 3.0에서는 아래 코드를 사용할 수 있습니다.

let textLabel = UILabel(frame: CGRect(x:containerView.frame.width/2 - 35, y: 
    containerView.frame.height/2 + 10, width: 70, height: 20))
    textLabel.text = "Add Text"
    textLabel.font = UIFont(name: "Helvetica", size: 15.0) // set fontName and Size
    textLabel.textAlignment = .center
    containerView.addSubview(textLabel) // containerView is a UIView

iOS 8의 경우

  static NSString *_myCustomFontName;

 + (NSString *)myCustomFontName:(NSString*)fontName
  {
 if ( !_myCustomFontName )
  {
   NSArray *arr = [UIFont fontNamesForFamilyName:fontName];
    // I know I only have one font in this family
    if ( [arr count] > 0 )
       _myCustomFontName = arr[0];
  }

 return _myCustomFontName;

 }

참고 URL : https://stackoverflow.com/questions/17575626/ios-set-font-size-of-uilabel-programmatically

반응형