iOS自定义按钮标题模糊

问题描述:

如果我在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]; 

我试过了所有我能想到的设置,没有任何东西似乎有所作为。任何想法我在这里失踪?

谢谢。

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

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

+0

是的,就是这样。我以为我已经检查过这些,但我猜不是。谢谢! – mickm

+0

欢迎来到Stack Overflow!请记住标记正确答案为已接受。 –

+0

谢谢!我如何标记它,以下是“此帖是否有用......”按钮? – mickm