2017-02-02 14 views
0

UITextField에는 두 개의 CAShapeLayers이 있습니다. 내 텍스트를 항상 중심에두고 크기가 안쪽의 흰색 원으로 제한하고 싶습니다.UITextField의 텍스트 크기 제한 및 크기 조절

내 동그라미 안에 텍스트의 크기를 제한하려면 어떻게해야합니까? 패딩을 사용하는 것이 가장 좋지만 텍스트가 항상 그 공간을 채우게하려면 어떻게해야합니까? 두 번째 부분 prob는 더 많은 텍스트가있는 경우 텍스트 글꼴 크기를 더 작게 설정하는 배율 인수와 관련이 있습니다.

여기 내 MWE입니다 : 내가 UITextView를 사용하는 것이 좋습니다 수 목적

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

, 그것은 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 이상 –