1

If I create a button in IB, and set only the type to be Custom, the Title and font size, it comes out nice and clear. If I create it programmatically, then the font comes out fuzzy. The code to create the button is:

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];

I've tried every setting I can think of, and nothing seems to make a difference. Any idea of what I'm missing here?

Thanks.

4

1 回答 1

6

您应该确保框架的xywidthheight是整数。round您可以使用/ floor/手动执行此操作ceil,或者CGRectIntegral如果您不需要那么多手动控制,请使用。您的按钮落在非整数像素边界上,可能是因为代码中的浮点除以 2。这是这个问题的副本。

请注意,它sizeWithFont:也可能返回非整数浮点数。

于 2012-11-19T22:31:20.200 回答