2017-02-02 3 views
0

のテキストのサイズを拡大縮小は、私は2つのCAShapeLayersUITextFieldを持っています。私は自分のテキストを常に中央に置いて、内側の白い円に(大きさで)制限したい。リミットで、UITextField

にはどうすればいいのパディングで最高の、その白い円内のテキストのサイズを制限するだけでなく、テキストは常にそのスペースを埋めることができますか? 2番目の部分のprobは、テキストが多い場合、テキストのフォントサイズを小さく設定するスケーリング係数と関係があります。ここで

は私の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) 
    } 
} 

答えて

0

その目的のために、私はUITextViewを使用して提案することができ、それはNSTextContainerを経由してそのためのネイティブサポートしています。 docs

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

あなたは、私はそれがあなたのために有用であろう期待し

valueLabel.adjustsFontSizeToFitWidth = true 
valueLabel.minimumFontSize = 0.5 

を試すことができます。

+0

minimumFontSizeは廃止されましたiOS 7 –

+0

BTWでは最小フォントサイズの制限を設定するには、フォントサイズの設定に適用する最小縮尺を設定する必要がありますminimumScaleFactorプロパティiOS 7+ –