Swift ios 如何在点击和更新时更改字体、字体颜色以及段控件中的一些其他属性
Swift ios how to change font , font color when tap and uptate , and some other properties in a segment control
我想使用段控件设计选项卡,但目前我对它所有属性的了解有限。第一张图片是我现在的第二张图片是我想要实现的
我真正想要实现的是段控件,其中包含项目 all 、 pending 、 expired 、 finished 。有没有人知道如何在段中实现项目之间的距离,还根据下面的设计示例更改字体颜色和样式。谢谢。
到目前为止我有什么。
let appearance = SMSegmentAppearance()
appearance.segmentOnSelectionColour = UIColor(red: 1, green: 0.8706, blue: 0.7608, alpha: 1.0)
appearance.segmentOffSelectionColour = UIColor(red: 1, green: 0.8706, blue: 0.7608, alpha: 1.0)
appearance.titleOnSelectionFont = UIFont.systemFont(ofSize: 25.0)
// segmentControlView.font = UIFont(name: "Avenir-Black", size: 12)
appearance.titleOffSelectionFont = UIFont.systemFont(ofSize: 25.0)
appearance.contentVerticalMargin = 10
/*
Init SMsegmentView
Set divider colour and width here if there is a need
*/
let segmentFrame = CGRect(x: self.margin, y: 5, width: self.segmentControlView.frame.size.width - self.margin*8, height: 50.0)
self.segmentView = SMSegmentView(frame: segmentFrame, dividerColour: UIColor(red: 1, green: 0.8706, blue: 0.7608, alpha: 1.0), dividerWidth: 1.0, segmentAppearance: appearance)
self.segmentView.backgroundColor = UIColor(red: 1, green: 0.8706, blue: 0.7608, alpha: 1.0)
self.segmentView.layer.cornerRadius = 5.0
self.segmentView.layer.borderColor = UIColor(red: 1, green: 0.8706, blue: 0.7608, alpha: 1.0).cgColor
self.segmentView.layer.borderWidth = 1.0
self.segmentView.addTarget(self, action: #selector(selectSegmentInSegmentView(segmentView:)), for: .valueChanged)
self.segmentView.addSegmentWithTitle("All", onSelectionImage: UIImage(named: ""), offSelectionImage: UIImage(named: ""))
self.segmentView.addSegmentWithTitle("Pending", onSelectionImage: UIImage(named: ""), offSelectionImage: UIImage(named: ""))
self.segmentView.addSegmentWithTitle("Finished", onSelectionImage: UIImage(named: ""), offSelectionImage: UIImage(named: ""))
self.segmentView.addSegmentWithTitle("Expired", onSelectionImage: UIImage(named: ""), offSelectionImage: UIImage(named: ""))
// var ownerNames = [String]()
//
// if fetchedToDoItems.count > 0 {
// for item in fetchedToDoItems {
// if item.houseNo == loggedInUserHouseNumber &&
// !ownerNames.contains(item.ownerName!) {
// ownerNames.append(item.ownerName!)
// }
// }
//
// for name in ownerNames {
// // Add segments
// self.segmentView.addSegmentWithTitle(name, onSelectionImage: UIImage(named: ""), offSelectionImage: UIImage(named: ""))
// }
//
// }
// Set segment with index 0 as selected by default
self.segmentView.selectedSegmentIndex = 0
self.segmentControlView.addSubview(self.segmentView)
正如我看到的文档:
您只需更改字体样式和大小:
你可以试试:
步骤:1 打印字体
func printFonts() {
let fontFamilyNames = UIFont.familyNames
for familyName in fontFamilyNames {
print("------------------------------")
print("Font Family Name = [\(familyName)]")
let names = UIFont.fontNames(forFamilyName: familyName)
print("Font Names = [\(names)]")
}
}
步骤:2 设置外观:-
appearance.titleOnSelectionFont = UIFont(name: "TradeGothicLTStd-Cn18", size: 10.0)!
appearance.titleOffSelectionFont = UIFont(name: "TradeGothicLTStd-Cn18", size: 10.0)!
希望对您有所帮助!
编辑以将选中的设置为黑色,未选中的设置为白色
appearance.segmentOffSelectionColour = UIColor.white //UnSelected Colour
appearance.segmentOnSelectionColour = UIColor.black //Selected Colour
我想使用段控件设计选项卡,但目前我对它所有属性的了解有限。第一张图片是我现在的第二张图片是我想要实现的
我真正想要实现的是段控件,其中包含项目 all 、 pending 、 expired 、 finished 。有没有人知道如何在段中实现项目之间的距离,还根据下面的设计示例更改字体颜色和样式。谢谢。
到目前为止我有什么。
let appearance = SMSegmentAppearance()
appearance.segmentOnSelectionColour = UIColor(red: 1, green: 0.8706, blue: 0.7608, alpha: 1.0)
appearance.segmentOffSelectionColour = UIColor(red: 1, green: 0.8706, blue: 0.7608, alpha: 1.0)
appearance.titleOnSelectionFont = UIFont.systemFont(ofSize: 25.0)
// segmentControlView.font = UIFont(name: "Avenir-Black", size: 12)
appearance.titleOffSelectionFont = UIFont.systemFont(ofSize: 25.0)
appearance.contentVerticalMargin = 10
/*
Init SMsegmentView
Set divider colour and width here if there is a need
*/
let segmentFrame = CGRect(x: self.margin, y: 5, width: self.segmentControlView.frame.size.width - self.margin*8, height: 50.0)
self.segmentView = SMSegmentView(frame: segmentFrame, dividerColour: UIColor(red: 1, green: 0.8706, blue: 0.7608, alpha: 1.0), dividerWidth: 1.0, segmentAppearance: appearance)
self.segmentView.backgroundColor = UIColor(red: 1, green: 0.8706, blue: 0.7608, alpha: 1.0)
self.segmentView.layer.cornerRadius = 5.0
self.segmentView.layer.borderColor = UIColor(red: 1, green: 0.8706, blue: 0.7608, alpha: 1.0).cgColor
self.segmentView.layer.borderWidth = 1.0
self.segmentView.addTarget(self, action: #selector(selectSegmentInSegmentView(segmentView:)), for: .valueChanged)
self.segmentView.addSegmentWithTitle("All", onSelectionImage: UIImage(named: ""), offSelectionImage: UIImage(named: ""))
self.segmentView.addSegmentWithTitle("Pending", onSelectionImage: UIImage(named: ""), offSelectionImage: UIImage(named: ""))
self.segmentView.addSegmentWithTitle("Finished", onSelectionImage: UIImage(named: ""), offSelectionImage: UIImage(named: ""))
self.segmentView.addSegmentWithTitle("Expired", onSelectionImage: UIImage(named: ""), offSelectionImage: UIImage(named: ""))
// var ownerNames = [String]()
//
// if fetchedToDoItems.count > 0 {
// for item in fetchedToDoItems {
// if item.houseNo == loggedInUserHouseNumber &&
// !ownerNames.contains(item.ownerName!) {
// ownerNames.append(item.ownerName!)
// }
// }
//
// for name in ownerNames {
// // Add segments
// self.segmentView.addSegmentWithTitle(name, onSelectionImage: UIImage(named: ""), offSelectionImage: UIImage(named: ""))
// }
//
// }
// Set segment with index 0 as selected by default
self.segmentView.selectedSegmentIndex = 0
self.segmentControlView.addSubview(self.segmentView)
正如我看到的文档:
您只需更改字体样式和大小:
你可以试试:
步骤:1 打印字体
func printFonts() {
let fontFamilyNames = UIFont.familyNames
for familyName in fontFamilyNames {
print("------------------------------")
print("Font Family Name = [\(familyName)]")
let names = UIFont.fontNames(forFamilyName: familyName)
print("Font Names = [\(names)]")
}
}
步骤:2 设置外观:-
appearance.titleOnSelectionFont = UIFont(name: "TradeGothicLTStd-Cn18", size: 10.0)!
appearance.titleOffSelectionFont = UIFont(name: "TradeGothicLTStd-Cn18", size: 10.0)!
希望对您有所帮助!
编辑以将选中的设置为黑色,未选中的设置为白色
appearance.segmentOffSelectionColour = UIColor.white //UnSelected Colour
appearance.segmentOnSelectionColour = UIColor.black //Selected Colour