UISegmentedControl의 글꼴 색을 변경하는 방법
다음을 위해 글꼴 색을 흰색에서 검은색으로 바꾸려고 합니다.UISegmentedControl
(iOS 4.*의 경우)
UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:itemTitle, nil]] autorelease];
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = [UIColor redColor];
for (id segment in [button subviews]) {
for (id label in [segment subviews]) {
if ([label isKindOfClass:[UILabel class]]) {
UILabel *titleLabel = (UILabel *) label;
[titleLabel setTextColor:[UIColor blackColor]];
}
}
}
UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
그러나 텍스트 색상은 변경되지 않습니다.텍스트 색을 변경하려면 다음 작업을 수행해야 합니다.UISegmentedControl
?
iOS 6.0 이상에서는 매우 간단합니다.
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:17], NSFontAttributeName,
[UIColor blackColor], NSForegroundColorAttributeName,
nil];
[_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
[_segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateSelected];
iOS 7에서 강조 표시된 세그먼트의 텍스트 색상을 변경해야 하는 경우 다음과 같은 해결책이 있습니다(찾는 데 시간이 좀 걸렸지만 이 게시물 덕분입니다).
목표-C
[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateSelected];
스위프트
let titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributes, forState: .Selected)
두 상태 모두 글꼴 색을 검은색으로 설정하는 코드
스위프트 5
let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .normal)
segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .selected)
스위프트 4
let titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]
segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .normal)
segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .selected)
다음은 글꼴을 굵은 면과 점 크기로 설정하는 코드입니다.
UIFont *Boldfont = [UIFont boldSystemFontOfSize:12.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:Boldfont
forKey: NSFontAttributeName];
[segmentedControl setTitleTextAttributes:attributes
forState:UIControlStateNormal];
도움이 되길 바랍니다.
iOS 14 및 Swift 5용으로 업데이트됨:
extension UISegmentedControl
{
func defaultConfiguration(font: UIFont = UIFont.systemFont(ofSize: 12), color: UIColor = UIColor.white)
{
let defaultAttributes = [
NSAttributedString.Key.font: font,
NSAttributedString.Key.foregroundColor: color
]
setTitleTextAttributes(defaultAttributes, for: .normal)
}
func selectedConfiguration(font: UIFont = UIFont.boldSystemFont(ofSize: 12), color: UIColor = UIColor.red)
{
let selectedAttributes = [
NSAttributedString.Key.font: font,
NSAttributedString.Key.foregroundColor: color
]
setTitleTextAttributes(selectedAttributes, for: .selected)
}
}
Swift 4용 업데이트 - 이 확장을 사용합니다(확장은 항상 훌륭하므로).!!)
extension UISegmentedControl {
func defaultConfiguration(font: UIFont = UIFont.systemFont(ofSize: 12), color: UIColor = UIColor.gray) {
let defaultAttributes = [
NSAttributedStringKey.font.rawValue: font,
NSAttributedStringKey.foregroundColor.rawValue: color
]
setTitleTextAttributes(defaultAttributes, for: .normal)
}
func selectedConfiguration(font: UIFont = UIFont.boldSystemFont(ofSize: 12), color: UIColor = UIColor.red) {
let selectedAttributes = [
NSAttributedStringKey.font.rawValue: font,
NSAttributedStringKey.foregroundColor.rawValue: color
]
setTitleTextAttributes(selectedAttributes, for: .selected)
}
}
그리고 각 클래스에서 이 기능을 사용할 수 있습니다.
@IBOutlet weak var segment: UISegmentedControl!
segment.defaultConfiguration()
// or provide paramater as per your requirement
segment.selectedConfiguration(color: .blue)
Swift 5에서는 이 구문으로 색상을 변경할 수 있습니다.
스위프트 5
let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
groupSegment.setTitleTextAttributes(titleTextAttributes, for: .selected)
스위프트 5 확장
extension UISegmentedControl {
func setTitleColor(_ color: UIColor, state: UIControl.State = .normal) {
var attributes = self.titleTextAttributes(for: state) ?? [:]
attributes[.foregroundColor] = color
self.setTitleTextAttributes(attributes, for: state)
}
func setTitleFont(_ font: UIFont, state: UIControl.State = .normal) {
var attributes = self.titleTextAttributes(for: state) ?? [:]
attributes[.font] = font
self.setTitleTextAttributes(attributes, for: state)
}
}
for (UIView *v in [[[segment subviews] objectAtIndex:0] subviews]) {
if ([v isKindOfClass:[UILabel class]]) {
UILabel *label=(UILabel *)[v retain];
lable.textColor=[UIColor blackColor];
}
}
iOS 3.0 이상의 경우
혹시라도 그곳에 도착해서 swift를 사용하여 일하는 다른 사람을 도와줄 수 있을지도 모릅니다.
저는 그것을 할 수 있는 두 가지 방법을 넣을 것입니다.에서 텍스트 특성을 변경할 수 있습니다.UIAppearance
또는 작업 중인 세그먼트에서 직접.
모양의 속성을 설정하는 첫 번째 예에서는 앱의 모든 세그먼트 컨트롤을 사용자 지정합니다.
let attributes = [ NSForegroundColorAttributeName : UIColor.grayColor(),
NSFontAttributeName : UIFont.systemFontOfSize(20)];
let attributesSelected = [ NSForegroundColorAttributeName : UIColor.blueColor(),
NSFontAttributeName : UIFont.systemFontOfSize(20)];
UISegmentedControl.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal)
UISegmentedControl.appearance().setTitleTextAttributes(attributesSelected, forState: UIControlState.Selected)
세그먼트에 직접 있는 두 번째 예제에서는 이 세그먼트만 사용자 정의합니다.
let attributes = [ NSForegroundColorAttributeName : UIColor.grayColor(),
NSFontAttributeName : UIFont.systemFontOfSize(20)];
let attributesSelected = [ NSForegroundColorAttributeName : UIColor.blueColor(),
NSFontAttributeName : UIFont.systemFontOfSize(20)];
segmented.setTitleTextAttributes(attributes, forState: UIControlState.Normal)
segmented.setTitleTextAttributes(attributesSelected, forState: UIControlState.Selected)
swift 3.2:
let attributes = [
NSFontAttributeName : bigTextFont,
NSForegroundColorAttributeName : UIColor.blue,
]
segmentedControl?.setTitleTextAttributes(attributes, for: .normal)
참고: 사용자 지정 배경색을 사용하면 모서리에 글리치가 표시되므로(색상이 외부 세그먼트를 채웁니다...), 다음 선을 사용하여 클리핑합니다.
segmentedControl!.layer.cornerRadius = 4.0
segmentedControl!.clipsToBounds = true
iOS 5.0 이상에서는 이 제목을 사용할 수 있습니다.사용자 지정할 텍스트 속성UISegmentedControl
객체:
NSDictionary *segmentedControlTextAttributes = @{NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue" size:18.0], NSForegroundColorAttributeName:[UIColor whiteColor]};
[self.segmentedControl setTitleTextAttributes:segmentedControlTextAttributes forState:UIControlStateNormal];
[self.segmentedControl setTitleTextAttributes:segmentedControlTextAttributes forState:UIControlStateHighlighted];
[self.segmentedControl setTitleTextAttributes:segmentedControlTextAttributes forState:UIControlStateSelected];
여기서 각 상태에 대한 글꼴을 사용자 정의 글꼴, 글꼴 크기, 색상으로 설정합니다.UISegmentedControl
.
UIS 세그먼트 제어 클래스 참조의 모양 사용자 지정 섹션에서 가능한 모든 단순 사용자 지정을 찾을 수 있습니다.
모노터치를 사용하고 있습니다.이유는 모르겠지만 View를 눌렀을 때 텍스트 색상은 변경되지 않습니다. 이 문제를 해결하려면 세그먼트 제어 수퍼뷰에 레이블을 추가한 다음 색상을 변경하십시오.
public static void SetColoredTittles(this UISegmentedControl s, string[] titles, UIColor selected, UIColor notSelected)
{
var segmentedLabels = new List<UILabel>();
float width = s.Frame.Width/titles.Length;
for (int i = 0; i < titles.Length; i++)
{
var frame = new RectangleF(s.Frame.X + i*width, s.Frame.Y, width,s.Frame.Height);
UILabel label = new UILabel(frame);
label.TextAlignment = UITextAlignment.Center;
label.BackgroundColor = UIColor.Clear;
label.Font = UIFont.BoldSystemFontOfSize(12f);
label.Text = titles[i];
s.Superview.AddSubview(label);
segmentedLabels.Add(label);
}
s.ValueChanged += delegate
{
TextColorChange(s,segmentedLabels, selected, notSelected);
};
TextColorChange(s,segmentedLabels, selected, notSelected);
}
static void TextColorChange(UISegmentedControl s, List<UILabel> segmentedLabels, UIColor selected, UIColor notSelected)
{
for (int i = 0; i < segmentedLabels.Count; i++)
{
if(i == s.SelectedSegment) segmentedLabels[i].TextColor = selected;
else segmentedLabels[i].TextColor = notSelected;
}
}
그런 다음 사용합니다.
segmented.SetColoredTittles(new string[] {
"text1",
"text2",
"text3"
}, UIColor.White,UIColor.DarkGray);
언급URL : https://stackoverflow.com/questions/9029760/how-to-change-font-color-of-uisegmentedcontrol
'programing' 카테고리의 다른 글
jQuery 키 누르기 후 입력 값 가져오기 (0) | 2023.08.30 |
---|---|
몇 초 후에 div 숨기기 (0) | 2023.08.30 |
Express가 멋지게 포맷된 HTML을 출력하려면 어떻게 해야 합니까? (0) | 2023.08.30 |
여러 열에서 내부 조인을 수행하는 방법 (0) | 2023.08.30 |
Excel 스프레드시트의 C#에서 시트 이름을 변경하는 방법 (0) | 2023.08.30 |