kiri
kiri

Reputation: 2007

How to Restrict Text for Button

I am using UIButton i am assigning some text to back button the text is dynamic and i may get bigger length How i can restrict text assigned to the Button some thing like 5 characters?

I am using below @"Some Text" is dynamic.

    UIButton *backButton = [UIButton buttonWithType:101];

    backButton.exclusiveTouch = YES;
    [backButton setTitle:**@"some text"** forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backBarButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

Upvotes: 1

Views: 79

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726919

To limit the string to 5 characters, use substringWithRange:

NSString *newTitle = [origTitle substringWithRange:NSMakeRange(0, min(5, origTitle.length))];

The call min(5, origTitle.length) picks the smaller of 5 and the length of the title that you want to set. You need to include <math.h> to use min function.

Upvotes: 1

Hemant Singh Rathore
Hemant Singh Rathore

Reputation: 2139

check length of your @"Some text" if desired condition true then set on your button

Upvotes: 0

Related Questions