[英]custom font on UIbutton title clipped on top of word
我已經上傳了一個自定義字體,並使用以下代碼將此字體應用於UI按鈕的標題
videoButton.titleLabel.font = [UIFont fontWithName:@"LaurenScript" size:20];
問題是標題被剪裁在第一個字母的頂部(見下圖)。 我嘗試在UIlabel上使用相同的字體,它工作正常,所以它不是字體的問題。 我還嘗試使用更改rectFrame
[videoButton.titleLabel setFrame:CGRectMake(0, 0, 300, 600)];
但那沒有做任何事情。 有誰知道如何解決這個問題? 干杯
我有一個類似的問題,在標題標簽的頂部切斷了分音符。 我創建了一個UIButton子類並使用此代碼來解決問題:
-(void)layoutSubviews
{
[super layoutSubviews];
CGRect frame = self.titleLabel.frame;
frame.size.height = self.bounds.size.height;
frame.origin.y = self.titleEdgeInsets.top;
self.titleLabel.frame = frame;
}
不確定這對任何人來說是否仍然存在問題,但我發現(使用自定義字體)上述解決方案並未最終解決問題,尤其是對於僅通過編程UIButton
創建的自定義UIButton
。
以下是我設法解決此問題的方法,特別是1行解決了剪輯問題:
UIButton *button = [[UIButton alloc] init];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"Nice!" forState:UIControlStateNormal];
[button setFont:[UIFont fontWithName:<CUSTOM FONT NAME> size:buttonWidth/3.0f]];
button = CGRectMake(0, 0, <WIDTH>, <HEIGHT>);
這是解決裁剪的線:
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentFill];
希望這可以幫助任何仍然被卡住的人。 快樂的編碼!
我在swift 2.1中嘗試這個,我從Antoine的答案中調整了這個代碼。 這可能不是很好的代碼,但它現在解決了我的問題。 你應該為自己做得更好。
import UIKit
class CustomUIButton: UIButton {
override func layoutSubviews() {
if var titleFrame : CGRect = titleLabel?.frame{
titleFrame.size = self.bounds.size
titleFrame.origin = CGPointZero
self.titleLabel!.frame = titleFrame
self.titleLabel!.textAlignment = .Center
}
}
}
有這個(悲傷的)解決方案: https : //stackoverflow.com/a/10200908/352628
我有一個類似的問題。 看起來titleLabel只是非常無法控制,為了獲得控制,你需要向按鈕注入一個UILabel子視圖...這讓我很難過:(
使用具有圖像的按鈕和具有自定義字體的文本具有相同的問題。 一切都必須垂直對齊。 並且圖像沒有拉伸。 這對我來說很好。
btn.contentVerticalAlignment = .fill
btn.contentMode = .center
btn.imageView?.contentMode = .scaleAspectFit
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.