У меня есть TextView, и мне нужно добавить кнопку над каждым словом с тем же размером и тем же шрифтом заголовка кнопки, что и в текущем слове.Этот код работает, но если в строке есть одно слово, например, это не так.
.h
@interface Text2ButtonsViewController : UIViewController {
UITextView *sourceText;
}
@property (nonatomic, retain) IBOutlet UITextView *sourceText;
.m
-(IBAction) processText {
for (UIView *button in [self.sourceText subviews]) {
if (button.tag == 33)
[button removeFromSuperview];
}
NSString *sourceString = [NSString stringWithString:self.sourceText.text];
NSArray *sourceArray = [sourceString componentsSeparatedByString:@" "];
CGSize spaceSize = [@" " sizeWithFont:self.sourceText.font];
float textPadding = 8.0f;
float stepX = textPadding;
float stepY = textPadding;
CGRect buttonFrame;
for (NSString *string in sourceArray) {
UIButton *actButton = [UIButton buttonWithType:UIButtonTypeCustom];
actButton.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.5];
[actButton setTitle:string forState:UIControlStateNormal];
actButton.titleLabel.font = self.sourceText.font;
CGSize stringSize = [string sizeWithFont:self.sourceText.font];
actButton.tag = 33;
//if summary width of all buttons and spaces are greater than
if (stepX + stringSize.width + textPadding > self.sourceText.frame.size.width) {
stepX = textPadding;
stepY = stepY + stringSize.height;
}
buttonFrame = CGRectMake(stepX, stepY, stringSize.width, stringSize.height);
stepX = stepX + stringSize.width + spaceSize.width;
actButton.frame = buttonFrame;
[self.sourceText addSubview:actButton];
}
}
Как определить, что слово является последним в строке UITextView?