- (void)focusedBtn:(NSInteger)number { _focusedBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 52, 52)]; NSString *title = @""; if (number >= 10000) { title = [NSString stringWithFormat:@"%.1f万",number/10000.0]; } else { title = [NSString stringWithFormat:@"%zd",number];
} [_focusedBtn setTitle:title forState:UIControlStateNormal]; UILabel *lab = [[UILabel alloc] init]; lab.text = @"1"; lab.font = [UIFont systemFontOfSize:12]; [lab sizeToFit]; NSLog(@"%@",NSStringFromCGSize(lab.bounds.size)); NSLog(@"buttuon:%@",NSStringFromCGRect(_focusedBtn.frame)); NSLog(@"imageView:%@",NSStringFromCGRect(_focusedBtn.imageView.frame)); NSLog(@"textLabel:%@",NSStringFromCGRect(_focusedBtn.titleLabel.frame)); NSLog(@"---------------------");
CGFloat buttonWidth = 52; CGFloat textWidth = 40; CGFloat imageWidth = 19; CGFloat imageHeight = 19; [_focusedBtn setImage:[UIImage imageNamed:@"focusBtn"] forState:UIControlStateNormal]; [_focusedBtn setBackgroundImage:[UIImage imageNamed:@"redcolor"] forState:UIControlStateNormal]; [_focusedBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; [_focusedBtn setContentVerticalAlignment:UIControlContentVerticalAlignmentTop]; [_focusedBtn setImageEdgeInsets:UIEdgeInsetsMake(6,(buttonWidth-imageWidth)/2, 0, 0 )]; [_focusedBtn.titleLabel setFont:[UIFont systemFontOfSize:12.0f]]; [_focusedBtn.titleLabel setContentMode:UIViewContentModeCenter]; _focusedBtn.titleLabel.textAlignment = NSTextAlignmentCenter; [_focusedBtn.titleLabel sizeToFit]; [_focusedBtn setTitleEdgeInsets:UIEdgeInsetsMake(imageHeight + 10 ,- _focusedBtn.titleLabel.bounds.size.width/2 + 7, 9,0)]; NSLog(@"buttuon:%@",NSStringFromCGRect(_focusedBtn.frame)); NSLog(@"imageView:%@",NSStringFromCGRect(_focusedBtn.imageView.frame)); NSLog(@"textLabel:%@",NSStringFromCGRect(_focusedBtn.titleLabel.frame)); [self.view addSubview:_focusedBtn]; }
|