UITextView - 如何定义一个单词是一行中的最后一个单词
我有一个 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行中的最后一个?
I have a TextView and I need to add a button above each word with the same size and the same font of the button title as in current word. This code works, but if line has one word for example it does not.
.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];
}
}
How to define that the word is the last one in the line of UITextView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在这里进行简单的条件检查。
编辑:
TextView 将包含空格和换行符。因此,您可以使用
componentsSeparatedByCharactersInSet:
方法来分隔文本。只是使用,我希望这应该有效。
You have to do a simple condition checking here.
Edit:
TextView will contain both white space and new line characters. So you can use the
componentsSeparatedByCharactersInSet:
method to separate the text. Just use,I hope this should work.
这段代码对我有用
This code works for me