나는이 UIBarButtonItem
내가에 UIToolbar
제목 완료 . 이제 Bold 를 사용하여 글꼴을 기본값에서 “Trebuchet MS” 로 변경하고 싶습니다 . 어떻게해야합니까?
답변
UIBarButtonItem은 UIBarItem에서 상속되므로 시도해 볼 수 있습니다
- (void)setTitleTextAttributes:(NSDictionary *)attributes
forState:(UIControlState)state
그러나 이것은 iOS5 전용입니다. iOS 3/4의 경우 사용자 정의보기를 사용해야합니다.
답변
정확히 말하면, 다음과 같이 할 수 있습니다
[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([
NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
NSAttributedStringKey.foregroundColor: UIColor.green],
for: .normal)
답변
앱 전체에서 글꼴의 UIAppearance
스타일을 지정 하는 데 관심 UIBarButtonItem
이있는 사용자는 다음 코드 행을 사용하여 수행 할 수 있습니다.
목표 C :
NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
스위프트 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
: 그림자 추가 (NSShadow
클래스 참조 참조)
(iOS7 + 용으로 업데이트)
답변
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];
답변
UIBarButtonItems
전부는 아니지만 일부를 위해이 작업을 수행하려면 다음 방법을 권장합니다.
UIBarButtonItem
서브 클래스를 작성하십시오 . 그것에 아무것도 추가하지 마십시오-스토리 보드 및 모양 프록시에서 사용자 정의 클래스로만 사용합니다 …- 스토리 보드에서 원하는 모든 클래스를
UIBarButtonItems
서브 클래스로 변경하십시오. - AppDelegate에서
UIBarButtonItem
서브 클래스를 가져 와서 다음 행을 추가하십시오.application:didFinishLaunchingWithOptions:
필자의 경우 UIBarButtonItem
텍스트를 굵게 표시하기 위해 하위 클래스로 분류 했습니다.
[[BoldBarButtonItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:18.0], NSFontAttributeName,nil]
forState:UIControlStateNormal];
