How to get visible text as a string on a textView in iOS? -
i have uitextview
. on textview have text. when resize (decreasing height of textview) textview
2 line text visible.
so how can visible text string. means how can split visible text main text string.
below images reference.
i need consectetaur cillium adipisicing pecu, sed eiusmod tempor incididunt ut labore et text string main string.
how can ?
yes got :) :)
in objective c
- (void)viewdidload { [super viewdidload]; [self getvisibletext]; // additional setup after loading view, typically 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]); }
in swift
override func viewdidload() { super.viewdidload() self.getvisibletext() // additional setup after loading view, typically nib. } func scrollviewdidscroll(scrollview: uiscrollview) { self.getvisibletext() } func getvisibletext() { let range: nsrange = self.visiblerangeoftextview(self.textview!) let mynsstring = self.textview!.text! 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 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) }
Comments
Post a Comment