背景画像として uibutton に追加したい画像が 1 つあります。ボタンの幅に合わせて画像を引き伸ばしたい。ユーザーは実行時にボタンのタイトルを入力します。したがって、ユーザーが入力したテキストの幅に応じて、画像が引き伸ばされます。ボタンの特定の幅を超えると、ボタンはそれ以上伸びませんが、代わりにテキストが切り捨てられます。これが私のコードです
CGSize suggestedSize = [self.strButtonTitle sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE]];
NSLog(@"Widht:- %f",suggestedSize.width);
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame = CGRectMake(50, 200, 75, 40);
CGRect frame = customButton.frame;
frame.size.width = suggestedSize.width;
if (frame.size.width > 75) {
frame.size.width = 75;
customButton.frame = frame;
}
else {
frame.size.width +=5;
customButton.frame = frame;
}
NSLog(@"Custom button width:- %f",customButton.frame.size.width);
UIImage *buttonImageNormal = [UIImage imageNamed:BUTTON_IMAGE];
UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[customButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
[customButton setTitle:self.strButtonTitle forState:UIControlStateNormal];
[customButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:customButton];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithCustomView:customButton];
self.navigationItem.leftBarButtonItem = barButton;
[barButton release];
私の問題は、ユーザーが入力したテキストに従ってボタンの幅が広がらないことです。ユーザーが ppppp と入力すると、幅が 75 まで増加せず、代わりに「pp...p」と表示されます。
どんな助けでも大歓迎です。ありがとう。