And*_*tto 3 uitabbar ios swift
我无法将紧排空间添加到选项卡栏属性文本中。有问题的 UITabBar 是一个自定义 tabBar,您可以找到下面的代码。
我正在使用“属性键”字典向项目标题添加属性,但我遇到了紧缩空间问题。
class ProfileTabBar: UITabBar {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setStyle()
}
required override init(frame: CGRect) {
super.init(frame: frame)
self.setStyle()
}
func setStyle() {
self.tintColor = Style.shared.primary1
// Disable the default border
self.layer.borderWidth = 0.0
self.clipsToBounds = true
// Create a new bottom border
let bottomLine = CALayer()
let screenWidth = UIScreen.main.bounds.width
//let viewForFrame = self.superview ?? self
//let screenWidth = viewForFrame.bounds.width
bottomLine.frame = CGRect(x: 0.0, y: self.frame.height - 1, width: screenWidth, height: 2.0)
bottomLine.backgroundColor = UIColor(red: 235.0/255, green: 235.0/255, blue: 235.0/255, alpha: 1.0).cgColor
self.layer.addSublayer(bottomLine)
// Get the size of a single item
let markerSize = CGSize(width: screenWidth/CGFloat(self.items!.count), height: self.frame.height)
// Create the selection indicator
self.selectionIndicatorImage = UIImage().createSelectionIndicator(color: self.tintColor, size: markerSize , lineWidth: 3.0)
// Customizing the items
if let items = self.items {
for item in items {
item.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -15)
let attributes: [NSAttributedStringKey : Any] = [
NSAttributedStringKey.font: UIFont(name: Style.shared.fontBold.fontName, size: 14) as Any,
NSAttributedStringKey.kern: NSNumber(value: 1.0)
]
item.setTitleTextAttributes(attributes, for: .normal)
}
}
}
Run Code Online (Sandbox Code Playgroud)
除了紧缩之外,所有属性都有效。我做错了什么?
这个问题很旧,这里有一个更旧的答案。看来UITabBarItem外表是无视的NSAttributedString.Key.kern。这给我们留下了一些选择。
子类化UITabBarItem这并不容易,因为UITabBarItem继承自的UIBarItem不是.NSObjectUIView
子类化UITabBar可以完成此操作,但仅涉及一些紧排的大量工作。您必须使用UIButton而不是UITabBarItem应用紧排。
您可以在标题中使用 unicode 字符添加间距。这非常简单,只需几行代码就可以实现您正在寻找的间距。
\nU+00201/4 厘米
U+20041/3 厘米
U+20051/4 厘米
U+20061/6 厘米
U+2008句点的宽度 \xe2\x80\x9c.\xe2\x80\x9d
U+20091/5 em(有时为 1/6 em)
String您可以像这样在 Swift 中的a 中使用 unicode 字符"\\u{2006}"。这意味着我们可以在 tabBarItem 标题中的所有字符之间插入一个小空格。像这样:
extension String {\n var withOneSixthEmSpacing: String {\n let letters = Array(self)\n return letters.map { String($0) + "\\u{2006}" }.joined()\n }\nRun Code Online (Sandbox Code Playgroud)\n将其用于我们的 tabBarItems:
\nself.navigationController.tabBarItem = UITabBarItem(\n title: "Home".withOneSixthEmSpacing,\n image: homeImage,\n selectedImage: homeSelectedImage\n)\nRun Code Online (Sandbox Code Playgroud)\n\n代替:
\n\n| 归档时间: |
|
| 查看次数: |
1042 次 |
| 最近记录: |