IT

iPhone / iPad의 UILabel에서 굵게 및 기울임 꼴을 어떻게 설정합니까?

lottoking 2020. 7. 3. 18:04
반응형

iPhone / iPad의 UILabel에서 굵게 및 기울임 꼴을 어떻게 설정합니까?


UILabeliPhone / iPad 에서 굵게 및 기울임 꼴을 어떻게 설정 합니까? 포럼을 검색했지만 아무런 도움이되지 않았습니다. 누구든지 나를 도울 수 있습니까?


sectionLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:18];

'fontWithName'속성 대신 설정할 수있는 글꼴 이름 목록이 있습니다.


서체 이름으로 연주하지 마십시오. 글꼴 설명자를 사용하면 이름이 필요하지 않습니다.

UILabel * label = [[UILabel alloc] init]; // use your label object instead of this
UIFontDescriptor * fontD = [label.font.fontDescriptor
            fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold
                            | UIFontDescriptorTraitItalic];
label.font = [UIFont fontWithDescriptor:fontD size:0];

size:0 '크기 그대로 유지'를 의미

Swift를 사용하여 다음 확장을 시도하십시오.

extension UIFont {

    func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
        let descriptor = self.fontDescriptor()
            .fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(traits))
        return UIFont(descriptor: descriptor, size: 0)
    }

    func boldItalic() -> UIFont {
        return withTraits(.TraitBold, .TraitItalic)
    }

}

그런 다음 다음과 같이 사용할 수 있습니다.

myLabel.font = myLabel.font.boldItalic()

또는 Condensed와 같은 추가 특성을 추가하십시오.

myLabel.font = myLabel.font.withTraits(.TraitCondensed, .TraitBold, .TraitItalic)

스위프트 4 업데이트 :

extension UIFont {
  var bold: UIFont {
    return with(traits: .traitBold)
  } // bold

  var italic: UIFont {
    return with(traits: .traitItalic)
  } // italic

  var boldItalic: UIFont {
    return with(traits: [.traitBold, .traitItalic])
  } // boldItalic


  func with(traits: UIFontDescriptorSymbolicTraits) -> UIFont {
    guard let descriptor = self.fontDescriptor.withSymbolicTraits(traits) else {
      return self
    } // guard

    return UIFont(descriptor: descriptor, size: 0)
  } // with(traits:)
} // extension

다음과 같이 사용하십시오.

myLabel.font = myLabel.font.bold

또는

myLabel.font = myLabel.font.italic

또는

myLabel.font = myLabel.font.with(traits: [ .traitBold, .traitCondensed ])

스위프트 5

    func with(_ traits: UIFontDescriptor.SymbolicTraits...) -> UIFont {
        guard let descriptor = self.fontDescriptor.withSymbolicTraits(UIFontDescriptor.SymbolicTraits(traits).union(self.fontDescriptor.symbolicTraits)) else {
            return self
        }
        return UIFont(descriptor: descriptor, size: 0)
    }

    func without(_ traits: UIFontDescriptor.SymbolicTraits...) -> UIFont {
        guard let descriptor = self.fontDescriptor.withSymbolicTraits(self.fontDescriptor.symbolicTraits.subtracting(UIFontDescriptor.SymbolicTraits(traits))) else {
            return self
        }
        return UIFont(descriptor: descriptor, size: 0)
    }

@Edinator에서 이것에 대해 살펴보십시오 ..

myLabel.font = [UIFont boldSystemFontOfSize:16.0f]
myLabel.font = [UIFont italicSystemFontOfSize:16.0f];

원하는 시간에 위 중 하나를 사용하십시오


글꼴 필드 에서 문자 "T"를 클릭하여 레이블의 글꼴 스타일, 패밀리, 크기를 설정할 수 있습니다 .

Label font settings


I have the same issue that need to apply both Bold and Italic on a label and button. You can simply use following code to achieve this effect:

myLabel.font = [UIFont fontWithName:@"Arial-BoldItalic" size:30.0];


With iOS 7 system default font, you'll be using helvetica neue bold if you are looking to keep system default font.

    [titleText setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0]];

Or you can simply call it:

    [titleText setFont:[UIFont boldSystemFontOfSize:16.0]];

Swift 3

Bold:

let bondFont = UIFont.boldSystemFont(ofSize:UIFont.labelFontSize)

Italic:

let italicFont = UIFont.italicSystemFont(ofSize:UIFont.labelFontSize)


have a look at: this URL


 btn.titleLabel.font=[UIFont fontWithName:@"Helvetica neue" size:10];
 btn.titleLabel.font =  [UIFont boldSystemFontOfSize:btnPrev.titleLabel.font.pointSize+3];

you can do bold label/button font also using this


I made a variation of the response of maksymilian wojakowski where you can add or remove a trait(s)

extension UIFont {

    func withTraits(_ traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
        let descriptor = self.fontDescriptor
            .withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits).union(self.fontDescriptor.symbolicTraits))
        return UIFont(descriptor: descriptor!, size: 0)
    }
    func withoutTraits(_ traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
        let descriptor = self.fontDescriptor
            .withSymbolicTraits(  self.fontDescriptor.symbolicTraits.subtracting(UIFontDescriptorSymbolicTraits(traits)))
        return UIFont(descriptor: descriptor!, size: 0)
    }
    func bold() -> UIFont {
        return withTraits( .traitBold)
    }

    func italic() -> UIFont {
        return withTraits(.traitItalic)
    }

    func noItalic() -> UIFont {
        return withoutTraits(.traitItalic)
    }
    func noBold() -> UIFont {
        return withoutTraits(.traitBold)
    }
}

exemple

label.font = label.font.italic().bold()

it useful when reusing cell and you want to remove the italic you put on a label in a previous cell


I recently wrote a blog post about the restrictions of the UIFont API and how to solve it. You can see it at here

With the code I provide there, you can get your desired UIFont as easy as:

UIFont *myFont = [FontResolver fontWithDescription:@"font-family: Helvetica; font-weight: bold; font-style: italic;"];

And then set it to your UILabel (or whatever) with: myLabel.font = myFont;


As has already been mentioned in these answers, you just need the right font name. I find that iOSFonts.com is the most helpful resource for knowing exactly what name to use.


Example Bold text:

UILabel *titleBold = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 30)];
UIFont* myBoldFont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[titleBold setFont:myBoldFont];

Example Italic text:

UILabel *subTitleItalic = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 200, 30)];
UIFont* myItalicFont = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
[subTitleItalic setFont:myItalicFont];

This is very simple. Here is the code.

[yourLabel setFont:[UIFont boldSystemFontOfSize:15.0];

This will help you.


Updating Maksymilian Wojakowski's awesome answer for swift 3

extension UIFont {

func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont? {
    guard let descriptorL = self.fontDescriptor.withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits)) else{
        return nil
    }
    return UIFont(descriptor: descriptorL, size: 0)
}

func boldItalic() -> UIFont? {
    return withTraits(traits: .traitBold, .traitItalic)
}

}


Although the answer provided by @tolbard is amazing and works well!

I feel creating an extension for something that can be achieved in just a line of code, would be an over kill.

You can get bold as well italic styling for the same text in your label by setting up the font property using UIFontDescriptor as shown below in the example below using Swift 4.0:

label.font = UIFont(descriptor: UIFontDescriptor().withSymbolicTraits([.traitBold, .traitItalic])!, size: 12)

Other options include:

traitLooseLeading
traitTightLeading
traitUIOptimized
traitVertical
traitMonoSpace
traitCondensed
traitExpanded

For more information on what those symbolic traits mean? visit here


Many times the bolded text is regarded in an information architecture way on another level and thus not have bolded and regular in one line, so you can split it to two labels/textViews, one regular and on bold italic. And use the editor to choose the font styles.


With Swift 5

For style = BOLD

label.font = UIFont(name:"HelveticaNeue-Bold", size: 15.0)

For style = Medium

label.font = UIFont(name:"HelveticaNeue-Medium", size: 15.0)

For style = Thin

label.font = UIFont(name:"HelveticaNeue-Thin", size: 15.0)

참고URL : https://stackoverflow.com/questions/4713236/how-do-i-set-bold-and-italic-on-uilabel-of-iphone-ipad

반응형