2012-11-19 94 views
1

如果我在IB中創建了一個按鈕,並且只將類型設置爲自定義,標題和字體大小,它會很好,清晰。如果我通過編程創建它,那麼字體會變得模糊。創建按鈕的代碼是:iOS自定義按鈕標題模糊

CGRect frame = self.sendButton.frame;

NSString *title = @"Read Disclosure"; 
    UIFont *font = [UIFont boldSystemFontOfSize:17.0]; 
    CGSize titleSize = [title sizeWithFont:font constrainedToSize:frame.size]; 

    CGFloat xValue = (self.view.frame.size.width - titleSize.width)/2; 
    CGFloat yValue = frame.origin.y - (titleSize.height/2); 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 

    [button setFrame:CGRectMake(xValue, yValue, titleSize.width, titleSize.height * 2)]; 
    [button setTitle:@"Read Disclosure" forState:UIControlStateNormal]; 
    [button.titleLabel setFont:font]; 
    [button setTitleColor:[UIColor colorWithRed:50.0/255.0 green:79.0/255.0 blue:133.0/255.0 alpha:1.0] forState:UIControlStateNormal]; 
    [button setTitleShadowColor:nil forState:UIControlStateNormal]; 
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; 
    [button addTarget:self action:@selector(disclosureButtonAction) forControlEvents:UIControlEventTouchUpInside]; 

    [self.view addSubview:button]; 

我試過了所有我能想到的設置,沒有任何東西似乎有所作爲。任何想法我在這裏失蹤?

謝謝。

回答

5

你應該確保你的幀的x,y,widthheight是整數。如果您不需要太多手動控制,您可以使用round/floor/ceil手動執行此操作,或使用CGRectIntegral。您的按鈕落在非整數像素邊界上,可能是因爲代碼中的浮點數除以2。這是this question的副本。

請注意,sizeWithFont:也可能會返回非整數浮點數。

+0

是的,就是這樣。我以爲我已經檢查過這些,但我猜不是。謝謝! – mickm

+0

歡迎來到Stack Overflow!請記住標記正確答案爲已接受。 –

+0

謝謝!我如何標記它,以下是「此帖是否有用......」按鈕? – mickm