簡體   English   中英

在UIButton中,titleLabel不能正確適合高度

[英]titleLabel can't fit in height correctly in UIButton

由於某些原因,我無法使titleLabel正確地放入UIButton中。

適合寬度,但不適合高度(由於baselineAdjustment可以工作)

let btn = UIButton(frame: CGRect(x: (CGFloat(index) * sizePerChar + marginWidthFirstLine / 2), y: marginHeight/2, width: sizePerChar, height: sizePerChar))
btn.setTitle("P", forState: .Normal)
btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
btn.titleLabel!.font = UIFont(name: "KohinoorDevanagari-Regular", size: 150)
btn.titleLabel!.adjustsFontSizeToFitWidth = true
btn.titleLabel!.minimumScaleFactor = 0.1
btn.titleLabel!.baselineAdjustment = .AlignCenters
btn.titleLabel!.textAlignment = .Center
btn.layer.cornerRadius = 10
btn.backgroundColor = UIColor.yellowColor()
self.view.addSubview(btn)

結果如下:

字母m較小且寬度合適,但其他所有字母的高度都太大了...

在此處輸入圖片說明

它應該工作:

btn.titleLabel!.adjustsFontSizeToFitWidth = false

設置字體后重設高度。

NSDictionary *屬性= [NSDictionary dictionaryWithObjectsAndKeys:yourButton.titleLabel.font,NSFontAttributeName,nil];

CGSize size = [@“虛擬文本” sizeWithAttributes:attributes];

yourbutton.frame.size.height = size.height;

這可能與您使用的字體有關。

UIFont(name: "KohinoorDevanagari-Regular", size: 150)

嘗試使用systemFont並測試其是否正常工作。 否則它應該起作用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM