迅速。 UISegmentedControl有两个不同的行

问题描述 投票:0回答:2

我需要使用两行和不同的字体和大小制作UISegmentedControl。

我设法使它有两条线,但我不知道如何制作不同的尺寸和字体。

for segment in segmentedControl.subviews{
    for label in segment.subviews{
        if let labels = label as? UILabel{
            labels.numberOfLines = 2


        }
    }
}
segmentedControl.setTitle("Hiragana \n ひらがな", forSegmentAt: 0)
segmentedControl.setTitle("Katakana \n カタカナ", forSegmentAt: 1)

现在我有这样的事情:

enter image description here

并希望这样:

enter image description here

swift uisegmentedcontrol
2个回答
0
投票

UISegmentedControl不支持像UILabel这样的属性标题,因此不支持具有两种不同字体的标题。

一种解决方案是从UIImage创建NSAttributedString,然后使用您的分段控件使用图像。

另一个选择是创建自己的(或找到第三方)自定义控件,使用属性标题。


0
投票

您无法通过apple提供的默认功能来实现

但你可以循环遍历子视图,因为你做的是行数

并添加

let attrString = NSMutableAttributedString(string: "Hiragana",
                                               attributes: [ 
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20) ])

    attrString.append(NSMutableAttributedString(string: "ひらがな",
                                                attributes: 
[NSAttributedString.Key.font: UIFont.systemFont(ofSize: 40) ]))

然后将其添加到标签中

label.attributedText = attrString 
© www.soinside.com 2019 - 2024. All rights reserved.