ios7 - Scale attributed text in a UITextView but retain frame width -
i'm trying scale editable attributed text in uitextview , adjust textview's width dynamically. have no problem scaling text uitextview within uiscrollview cannot work out alter in order retain textview's width.
the above text start with. scale , cropped uiscrollview's bounds.
but able retain original width , reflow text in image.
i appreciate help on this. using autolayout, ios 7 , xcode 5.
i have tried scaling fonts within string there buffering bug in ios7 means uigesturerecognizerstatechanged notifications buffered , scaling continues long after pinch gesture has ended attributed strings more few font changes. interested botch have @ moment scale fonts not elegantly @ all.
- (void)scaletextview:(uipinchgesturerecognizer *)pinchgesturerecognizer { cgfloat scale = 0; nsmutableattributedstring *string; switch (pinchgesturerecognizer.state) { case uigesturerecognizerstatebegan: self.old_scale = 1.0; self.last_time = [nsdate date]; break; case uigesturerecognizerstatechanged: scale = pinchgesturerecognizer.scale - self.old_scale; if( [self.last_time timeintervalsincenow] < -0.2 ) { // updating 5 times sec best can - faster , buffered changes going on ages! self.last_time = [nsdate date]; string = [self getscaledstringfrom:[self.textview.attributedtext mutablecopy] withscale:1.0 + scale]; if( string ) { self.textview.attributedtext = string; self.old_scale = pinchgesturerecognizer.scale; } } break; case uigesturerecognizerstateended: case uigesturerecognizerstatecancelled: case uigesturerecognizerstatefailed: break; default: break; } } - (nsmutableattributedstring*) getscaledstringfrom:(nsmutableattributedstring*)string withscale:(cgfloat)scale { [string beginediting]; [string enumerateattribute:nsfontattributename inrange:nsmakerange(0, string.length) options:0 usingblock:^(id value, nsrange range, bool *stop) { if (value) { uifont *oldfont = (uifont *)value; uifont *newfont = [oldfont fontwithsize:oldfont.pointsize * scale]; [string removeattribute:nsfontattributename range:range]; [string addattribute:nsfontattributename value:newfont range:range]; } }]; [string endediting]; homecoming string; } what looking frame need alter in scrollview, textview, textcontainer, layoutmanager or whatever cause string reflow within adjusted border when textview within has been scaled/zoomed. alternatively, if can right or improve code more performant i'd extremely grateful.
how using textcontainerinset ?
so (assuming width textview original width)
cgfloat rightinset = width * (pinchgesturerecognizer.scale - 1.); self.textview.textcontainerinset = uiedgeinsetsmake(0, 0, 0, rightinset); (i did not test this, allow me know if works)
ios7 uiscrollview uitextview autolayout nsattributedstring
No comments:
Post a Comment