gpt4 book ai didi

ios - UISegmentedControl 更改 ONE 段的字体

转载 作者:行者123 更新时间:2023-11-30 12:28:32 25 4
gpt4 key购买 nike

如何更改 UISegmentedControl 中一个片段的字体?

我知道如何为整个控件执行此操作。但如果用户有某些东西,我想突出显示其中的一个部分。然而,这个没有被选中。

注意:我使用的是 swift 3

谢谢!

最佳答案

好的,所以评论是正确的。我需要该部分的图像。我拼凑了一些答案来得到这个:

DispatchQueue.main.async(execute: {

//custom funct to determine the index / just use your index
guard let segIndexL = self.navMode?.determineIndexForMode(navType: navType) else {

return
}


let width: CGFloat = 29.0
let height: CGFloat = 24.0 //height (28) minus 2pt border for top and bottom
let size = CGSize(width: width, height: height)


UIGraphicsBeginImageContextWithOptions(size, false, 0)

var fontSize: CGFloat? = nil

for segment in self.segSubViewNavControl.subviews {
for label in segment.subviews {
if label is UILabel {
fontSize = (label as! UILabel).font.pointSize + 1.0;
break
}

if fontSize != nil {
break
}
}
}

guard let fontSizeL = fontSize else {
return
}

let font = UIFont.boldSystemFont(ofSize: fontSizeL)

let txtAttributes = [
NSFontAttributeName : font,
NSForegroundColorAttributeName : UIColor.red
] as [String : Any]

guard let titleL = self.segSubViewNavControl.titleForSegment(at: segIndexL) else {
return
}

let stringSizeL = titleL.size(attributes: txtAttributes)

let rectText = CGRect(x: (width - stringSizeL.width) / 2, y: (height - stringSizeL.height) / 2, width: stringSizeL.width, height: stringSizeL.height)


titleL.draw(
in: rectText,
withAttributes: txtAttributes
)

let newImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

self.segSubViewNavControl.setImage(newImage, forSegmentAt: segIndexL)


})

我从这里得到了这些答案:

iOS Drawing text in the centre of shapes

How to make text appear in centre of the image?

How to Create a CGSize in Swift?

How do I add text to an image in iOS Swift

Using isKindOf in Swift 3

谢谢!

关于ios - UISegmentedControl 更改 ONE 段的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43871378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com