iOS自定义按钮标题模糊

时间:2012-11-19 22:22:14

标签: ios button title blurry

如果我在IB中创建一个按钮,并且只将类型设置为自定义,标题和字体大小,它会很好而且清晰。如果我以编程方式创建它,那么字体就会模糊不清。创建按钮的代码是:

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

我已经尝试过我能想到的每一个设置,似乎没有任何区别。知道我在这里缺少什么吗?

感谢。

1 个答案:

答案 0 :(得分:6)

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

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