如何在iOS中的textView上以字符串形式获取可见文本? [英] How to get visible text as a string on a textView in iOS?

查看:26
本文介绍了如何在iOS中的textView上以字符串形式获取可见文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITextView.在那个文本视图上,我有一些文本.但是当我调整大小(降低 textview 的高度)TextView 然后只有 2 行文本可见.

I have an UITextView. On that textview I have some text. But when I resize (decreasing the height of textview) the TextView then only 2 line text is visible.

那么如何将可见文本作为字符串获取.我的意思是如何从主文本字符串中拆分可见文本.

So How can I get that visible text as a string. I means how can I split that visible text from main text string.

以下图片仅供参考.

我只需要 consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut laboure et 文本作为主字符串中的一个字符串.

I need only consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et text as a string from main string.

我怎样才能得到它?

推荐答案

是的,我明白了 :) :)

Yes I got it :) :)

在目标 C

- (void)viewDidLoad {
     [super viewDidLoad];
     [self getVisibleText];

     // Do any additional setup after loading the view, typically from a nib.
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self getVisibleText];
}

- (void)getVisibleText {
    NSRange range = [self visibleRangeOfTextView:self.textview];
    NSString *visibleText = [self.textview.text substringWithRange:range];
    NSLog(@"visibleText == %@",visibleText);
}

- (NSRange)visibleRangeOfTextView:(UITextView *)textView {
    CGRect bounds = textView.bounds;
    UITextPosition *start = [textView characterRangeAtPoint:bounds.origin].start;
    UITextPosition *end = [textView characterRangeAtPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))].end;
    return NSMakeRange([textView offsetFromPosition:textView.beginningOfDocument toPosition:start],
                   [textView offsetFromPosition:start toPosition:end]);
}

在 Swift 中

    override func viewDidLoad() {
    super.viewDidLoad()

    self.getVisibleText()

    // Do any additional setup after loading the view, typically from a nib.
}

func scrollViewDidScroll(scrollView: UIScrollView) {
    self.getVisibleText()
}

func getVisibleText() {
    let range: NSRange = self.visibleRangeOfTextView(self.textview!)
    let myNSString = self.textview!.text! as NSString
    let visibleText: String = myNSString.substringWithRange(range)
    NSLog("visibleText == %@", visibleText)
    self.lable!.text = visibleText
}

private func visibleRangeOfTextView(textView: UITextView) -> NSRange {
    let bounds = textView.bounds
    let origin = CGPointMake(10,10) //Overcome the default UITextView left/top margin
    let startCharacterRange = textView.characterRangeAtPoint(origin)
    if startCharacterRange == nil {
        return NSMakeRange(0,0)
    }
    let startPosition = textView.characterRangeAtPoint(origin)!.start

    let endCharacterRange = textView.characterRangeAtPoint(CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds)))
    if endCharacterRange == nil {
        return NSMakeRange(0,0)
    }
    let endPosition = textView.characterRangeAtPoint(CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds)))!.end

    let startIndex = textView.offsetFromPosition(textView.beginningOfDocument, toPosition: startPosition)
    let endIndex = textView.offsetFromPosition(startPosition, toPosition: endPosition)
    return NSMakeRange(startIndex, endIndex)
}

这篇关于如何在iOS中的textView上以字符串形式获取可见文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆