gpt4 book ai didi

ios - 限制和缩放 UITextField 中文本的大小

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

我有一个带有两个 CAShapeLayersUITextField。我希望我的文本始终居中并限制(大小)到内部白色圆圈。

如何限制白色圆圈内文本的大小(最好使用填充),同时又使文本始终填充该空间?第二部分问题与缩放因子有关,如果有更多文本,该缩放因子会将文本字体大小设置得更小。

这是我的 MWE:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

self.view.backgroundColor = .darkGray

let size:CGFloat = 300.0
let centerPoint:CGFloat = 200.0

let valueLabel = UITextField()
valueLabel.isUserInteractionEnabled = false
valueLabel.contentVerticalAlignment = .center
valueLabel.textAlignment = .center
valueLabel.text = "300"
valueLabel.textColor = .black

valueLabel.font = UIFont.init(name: "HelveticaNeue-Medium", size: 100)
valueLabel.bounds = CGRect(x:0.0, y:0.0, width:size, height:size)
valueLabel.center = CGPoint(x:centerPoint, y:centerPoint)

let redCircle:CAShapeLayer = CAShapeLayer()
redCircle.path = UIBezierPath(ovalIn: valueLabel.bounds).cgPath
redCircle.fillColor = UIColor.red.cgColor
redCircle.strokeColor = UIColor.white.cgColor
redCircle.lineWidth = 10

valueLabel.layer.addSublayer(redCircle)

let whiteCircle:CAShapeLayer = CAShapeLayer()
let tmpRect = CGRect(x:valueLabel.bounds.origin.x,y:valueLabel.bounds.origin.x,width:valueLabel.bounds.width-80.0,height:valueLabel.bounds.height-80.0)
whiteCircle.path = UIBezierPath(ovalIn: tmpRect).cgPath
whiteCircle.fillColor = UIColor.white.cgColor
whiteCircle.strokeColor = UIColor.white.cgColor
whiteCircle.lineWidth = 10
let posX = valueLabel.bounds.midX - (size-80.0)/2.0
let posY = valueLabel.bounds.midY - (size-80.0)/2.0
whiteCircle.position = CGPoint(x:posX, y:posY)
valueLabel.layer.addSublayer(whiteCircle)

self.view.addSubview(valueLabel)
}
}

最佳答案

为此,我建议使用 UITextView,它通过 NSTextContainer 对此提供 native 支持。 docs

textView.textContainer.exclusionPaths = [..] // array of UIBezierPaths

关于ios - 限制和缩放 UITextField 中文本的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42014155/

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