bestsource

UIBarButtonItem 글꼴 변경

bestsource 2023. 6. 13. 22:27
반응형

UIBarButtonItem 글꼴 변경

UIBarButtonItem이 있는 UI 도구 모음

나는 있습니다UIBarButtonItem내 안에서UIToolbar완료라는 제목의이제 글꼴을 기본 글꼴에서 볼드가 있는 "Trebuchet MS"로 변경하고 싶습니다.내가 어떻게 그럴 수 있을까?

정확히 말하면, 이것은 아래와 같이 할 수 있습니다.

[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
    [UIFont fontWithName:@"Helvetica-Bold" size:26.0], NSFontAttributeName,
    [UIColor greenColor], NSForegroundColorAttributeName,
    nil] 
                          forState:UIControlStateNormal];

또는 객체 리터럴 구문을 사용하는 경우:

[buttonItem setTitleTextAttributes:@{
     NSFontAttributeName: [UIFont fontWithName:@"Helvetica-Bold" size:26.0],
     NSForegroundColorAttributeName: [UIColor greenColor]
} forState:UIControlStateNormal];

편의를 위해 Swift를 구현하면 다음과 같습니다.

buttonItem.setTitleTextAttributes([
        NSAttributedString.Key.font : UIFont.regularPoppinsFont(withSize: 26),
        NSAttributedString.Key.foregroundColor : UIColor.white],
                                     for: .normal)

사용에 관심이 있는 사용자용UIAppearance그들의 스타일을 짜다UIBarButtonItem앱 전체의 글꼴로, 다음 코드 라인을 사용하여 수행할 수 있습니다.

목표 C:

NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];

Swift 2.3:

UIBarButtonItem.appearance().setTitleTextAttributes(
[
    NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
    NSForegroundColorAttributeName : UIColor.white
],
for: .normal)

스위프트 3

UIBarButtonItem.appearance().setTitleTextAttributes(
[
    NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
    NSForegroundColorAttributeName : UIColor.white,
], for: .normal)

스위프트 4

UIBarButtonItem.appearance().setTitleTextAttributes(
[
    NSAttributedStringKey.font : UIFont(name: "HelveticaNeue-Light", size: 12)!,
    NSAttributedStringKey.foregroundColor : UIColor.white,
], for: .normal)

또는 단일 UIBarButtonItem(모든 앱에 해당되지 않음)의 경우, 특히 단추 하나에 대한 사용자 정의 글꼴이 있는 경우:

스위프트 3

let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
    NSFontAttributeName : UIFont(name: "FontAwesome", size: 26)!,
    NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"

스위프트 4

let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
    NSAttributedStringKey.font : UIFont(name: "FontAwesome", size: 26)!,
    NSAttributedStringKey.foregroundColor : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"

물론 글꼴과 크기는 원하는 대로 변경할 수 있습니다.나는 이 코드를 더 선호합니다.AppDelegate.m에 철하다.didFinishLaunchingWithOptions부분.

사용 가능한 특성(에 추가하기만 하면 됨)NSDictionary):

  • NSFontAttributeName를 사용하여 글꼴 변경UIFont
  • NSForegroundColorAttributeName를 사용하여 색상 변경UIColor
  • NSShadow드롭 섀도 추가(클래스 참조 참조)

(iOS7+용으로 업데이트됨)

UIBarButtonItem은 UIBarItem에서 상속되므로 시도할 수 있습니다.

- (void)setTitleTextAttributes:(NSDictionary *)attributes
                  forState:(UIControlState)state

iOS5 전용입니다.iOS 3/4의 경우 사용자 정의 보기를 사용해야 합니다.

Swift에서는 다음과 같이 작업할 수 있습니다.

backButtonItem.setTitleTextAttributes([
        NSFontAttributeName : UIFont(name: "Helvetica-Bold", size: 26)!,
        NSForegroundColorAttributeName : UIColor.blackColor()],
    forState: UIControlState.Normal)

위의 훌륭한 답변들입니다.iOS7 업데이트 중:

NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Thin" size:18.0] , NSForegroundColorAttributeName: [UIColor whiteColor]};
    [[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];

스위프트3

  buttonName.setAttributedTitle([
        NSFontAttributeName : UIFont.systemFontOfSize(18.0),
        NSForegroundColorAttributeName : UIColor.red,NSBackgroundColorAttributeName:UIColor.black],
                                     forState: UIControlState.Normal)

재빠른

   barbutton.setTitleTextAttributes([
        NSFontAttributeName : UIFont.systemFontOfSize(18.0),
        NSForegroundColorAttributeName : UIColor.redColor(),NSBackgroundColorAttributeName:UIColor.blackColor()],
        forState: UIControlState.Normal)

목표-C

     [ barbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
        [UIFont fontWithName:@"Helvetica-Bold" size:20.0], NSFontAttributeName,
        [UIColor redColor], NSForegroundColorAttributeName,[UIColor blackColor],NSBackgroundColorAttributeName,
        nil]
        forState:UIControlStateNormal];

스위프트 5 구현

rightButtonItem.setTitleTextAttributes([
            NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
            NSAttributedString.Key.foregroundColor: UIColor.green],
        for: .normal)

일부 사용자를 위해 이 작업을 수행하려면UIBarButtonItems하지만 제가 다음과 같은 접근법을 추천하는 것은 아닙니다.

  1. 작성UIBarButtonItem아류의여기에 아무것도 추가하지 마십시오. 스토리보드에서 사용자 지정 클래스 및 모양 프록시로만 사용할 수 있습니다.
  2. 스토리보드에서 원하는 모든 사용자 지정 클래스 변경UIBarButtonItems당신의 하위 클래스로
  3. AppDelegate에서 다음 항목을 가져옵니다.UIBarButtonItem하위 클래스 및 다음 행 추가application:didFinishLaunchingWithOptions:

저 같은 경우에는 하위 분류를 했습니다.UIBarButtonItem텍스트를 굵게 표시하는 유일한 목적:

[[BoldBarButtonItem appearance] setTitleTextAttributes:
  [NSDictionary dictionaryWithObjectsAndKeys: 
    [UIFont boldSystemFontOfSize:18.0], NSFontAttributeName,nil] 
                                              forState:UIControlStateNormal];

Swift 4에서는 다음의 글꼴과 색상을 변경할 수 있습니다.UIBarButtonItem다음 코드를 추가함으로써.

addTodoBarButton.setTitleTextAttributes(
        [
        NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 17)!,
        NSAttributedStringKey.foregroundColor: UIColor.black
        ], for: .normal)

올바른 방법입니다. barButtonItem(이 경우 오른쪽 BarButtonItem)을 선언하고 setTitleTextAttributes를 추가합니다.

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Go!", style: .plain, target: self, action: #selector(yourFuncDestination))

제목 속성을 추가할 수 있는 후

navigationItem.rightBarButtonItem?.setTitleTextAttributes([.font : UIFont.systemFont(ofSize: 18, weight: .bold), .foregroundColor : UIColor.white], for: .normal)

크기, 무게(.bold, .heavy, .regular 등) 및 원하는 색상을 변경할 수 있습니다.이 도움을 바랍니다 :)

swift 3

barButtonName.setTitleTextAttributes( [NSFontAttributeName : UIFont.systemFont(ofSize: 18.0),NSForegroundColorAttributeName : UIColor.white], for: .normal) 

앱 전체:

if let font = UIFont(name: "AvenirNext-DemiBold", size: 15) {
        UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName:TOOLBAR_TITLE_COLOR], forState: UIControlState.Normal)

    }

UIBarButton글꼴 변경과 관련된 속성이 없습니다.그러나 사용자 정의 글꼴로 단추를 만든 다음 UIBarButton에 추가할 수 있습니다.당신의 문제가 해결될 수도 있습니다.

버전을 iOS4를 .initWithCustomView:글꼴을 쉽게 사용자 지정할 수 있는 UI 단추와 같은 자신만의 보기를 제공합니다.

프로그래밍 방식 대신 드래그 앤 드롭으로 단추를 만들려면 UIButton을 Interface Builder의 도구 모음이나 탐색 모음으로 끌 수도 있습니다.

안타깝게도 이것은 버튼 배경 이미지를 직접 만드는 것을 의미합니다.iOS5 이전에는 표준 UIBarButtonItem 글꼴을 사용자 지정할 수 없습니다.

사용자 정의를 생성할 수 있습니다.UIView프로그래밍 방식:

UIView *buttonItemView = [[UIView alloc] initWithFrame:buttonFrame];

그런 다음 이미지, 레이블 또는 원하는 것을 사용자 정의 보기에 추가합니다.

[buttonItemView addSubview:customImage];

[buttonItemView addSubview:customLabel];

...

이제 그것을 당신의 것에 넣으세요.UIBarButtomItem.

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonItemView];

그리고 마지막으로 탐색 모음에 barButtonItem을 추가합니다.

완성을 위해 2019년 목표-C에서 여전히 사용되는 이 방법을 추가하고 싶습니다.:)

_titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_titleLabel.text = _titleBarButtonItem.title;
_titleLabel.textColor = UIColor.whiteColor;
_titleLabel.font = [UtilityMethods appFontProDisplayBold:26.0];

[_titleLabel sizeToFit];
UIBarButtonItem *titleLabelItem = [[UIBarButtonItem alloc] initWithCustomView:_titleLabel];

언급URL : https://stackoverflow.com/questions/8849913/change-the-font-of-a-uibarbuttonitem

반응형